dynamic naming of datasets

3 次查看(过去 30 天)
Marco Mambelli
Marco Mambelli 2015-12-29
编辑: Stephen23 2015-12-31
Hi,
Long story short my script uploads into MATLAB a number of Excel files, using a for cycle, and saves them in the following way:
S is a structure, it's fields are A1 A2 ... and they are also structures (I shall call them substructures from now on, they are one for every excel file I upload). The fields of these substructures are say x, y, z (3 cell arrays). The way my function works A1, A2, ... are name dynamically depending on the date of the file I'm uploading (I have one for every date, think like H151221 H151222 and so on), while the substructure's fields are always the same (defined by the uploading function, so I'll have H151221.z H152122.z etc.).
What I would also like to do is to save S.H151221.z as a dataset using cell2dataset or struct2datset. My big problem lies in the fact that I cannot create a dynamically named dataset that will allow me to call it say Dat_H151221.
Any hints? I know dynamic variable creation is shamed upon but I can't see any way around it since I'm uploading 100 different files and need a dataset for each of them.
Thanks!
  2 个评论
Walter Roberson
Walter Roberson 2015-12-29
You should be considering switching to table() from dataset()
Stephen23
Stephen23 2015-12-31
编辑:Stephen23 2015-12-31
"dynamic variable creation is shamed upon": for those readers who do not know why, and still think that creating variable names dynamically is a great idea, please read this:

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2015-12-29
You can store your datasets as fields in a structure.
Sd = structfun(@struct2datset, S, 'Uniform', 0);

Image Analyst
Image Analyst 2015-12-29
I agree with Walter - you should use a table. In fact it's pretty easy to just load your Excel file into a table immediately upon importing:
for k = 1 : numberOfFiles
thisFileName = fullfile(.............
t(k) = readtable(thisFileName; % Instead of xlsread() use readtable()
end
Then if you want to save your array of tables, just use save()
save('MyExcelTables.mat', t't';

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by