assign new variable name from file name
17 views (last 30 days)
Show older comments
Hello,
I want to import tables and matrixes. since they are several of them, i would like that the name of the table or matrix created takes the name of the selected file.
my code ends by assigning a new varible of name data, but i would like that instead of data it is called the same way as the file selected
%%IMPORT File
[baseFileName,folder]=uigetfile('*.*');
fullFileName=fullfile(folder, baseFileName);
filename = fullFileName;
delimiter = ';';
startRow = 2;
formatSpec = '%C%{HH:mm:ss}D%f%f%f%f%f%f%f%f%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to the format.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%%Close the text file.
fclose(fileID);
%%Create output variable
s=importdata(baseFileName);
data= table(dataArray{1:end-1}, 'VariableNames', {'Date','Time','Position','Xc','Yc','u_1e','u_FWHM','v_1e','v_FWHM','Orientation'});
%%Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
4 Comments
Stephen23
on 22 Jun 2018
i wanted that the variable takes the name of the file, because i have measurements from different samples that all have the same name, each saved into a different folder. in order to load all of them and compare them, i need to be able to know where each variable comes from. therefore i wanted that the created table or matrix has the name of the file(or folder, any of both)
Stephen23
on 22 Jun 2018
Edited: Stephen23
on 22 Jun 2018
@Eduardo Lozano: keeping meta-data, like the filenames, or test descriptions, or anything else you might dream of, does not require defining each imported table to have its own separate variable. In fact, that would be very a bad way of writing code, because mixing meta-data and variable names is always a bad idea, making code slow, complex, buggy, and hard to debug. Read this to know some of the reasons why:
Of course it is easy to store meta-data by simply treating it as real data (which it is), and there is absolutely nothing stopping you from doing so, using much better ways of writing code than what you are trying to do.
A simple, standard method would be to use one or two cell arrays:
S = dir(...);
N = {S.name};
D = N; % preallocate
for k = 1:numel(S)
fid = fopen(N{k},'rt');
D{k} = textscan(fid,...);
fclose(fid);
...
end
So you have all of the data in the cell array D, and the corresponding filenames in the cell array N. You can trivially loop over them both (like the loop above does), or process those cell arrays directly (e.g. using strcmp, regexp on the name cell array).
This is much simpler and more efficient than what you are trying to do, and gives exactly the information that you need, without the complexity and problems of magically accessing variable names:
Just remember, meta-data (like filenames and folder names) is data, so it does NOT belong in the names of your MATLAB variables.
Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!