assign new variable name from file name
显示 更早的评论
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 个评论
Rik
2018-6-22
Why do you want to change the name? Presumably you want to use it later on. Changing the name will make that more difficult.
Metadata is data itself. You could consider a struct with a data field and a field with the source file name. That way you'll avoid the minefield that bears the name eval. (see this page as well)
"..but i would like that instead of data it is called the same way as the file selected"
Why do you want to do that? Dynamically defining variable names just forces you into writing slow, buggy, complex code that is hard to debug. Read this to know more:
If you need to load one file at-a-time into MATLAB memory then just use the same variable names. If need to load all files into memory then use indexing: indexing is very simple, very neat, and extremely efficient. Indexing is what all experienced MATLAB users would use, and there is no reason why you can't too.
Stephen23
2018-6-22
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)
@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.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!