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