dynamic naming of datasets

3 views (last 30 days)
Marco Mambelli
Marco Mambelli on 29 Dec 2015
Edited: Stephen23 on 31 Dec 2015
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 Comments
Walter Roberson
Walter Roberson on 29 Dec 2015
You should be considering switching to table() from dataset()
Stephen23
Stephen23 on 31 Dec 2015
Edited: Stephen23 on 31 Dec 2015
"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:

Sign in to comment.

Answers (2)

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

Image Analyst
Image Analyst on 29 Dec 2015
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';

Categories

Find more on Data Import from MATLAB 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!