Sorting a cell containing a set of structures?
3 views (last 30 days)
Show older comments
Hello everyone,
I am currently working on my final-year individual project at university, where I'm doing a potentially large array of experimental tests (in a wind tunnel). I would like to then organise all my data for post-processing.
As things stand for the moment, this is how I've set up my post-processing in Matlab:
-> output from WT tests in the form of an excel file -> all excel files moved to one location -> list of excel files read
I then create a cell named dat, in which I create a structure for each experiment I conduct, meaning this is the structure of my code:
for i=1:length(excelList)
dat{i}.R=excelList(i).name(4);
dat{i}.M=xlsread(excelList(i,1).name);
dat{i}.var1=dat{i}.M(...);
dat{i}.var2=dat{i}.M(...);
dat{i}.var3=dat{i}.M(...);
end
In the code posted above, the variable R is a string containing the number of the run (i.e. run 1, run 2, etc) for which I'm storing data in cell i of dat.
This is where I've hit a snag. As my different runs represent a chronological evolution configurations, I'd like my cell (dat) to store my data with run 1 being the first structure in the cell, run 2 the second structure, and so on.
I've so far found methods of sorting a cell, as well as methods to sort a structure, but none that sort a cell of structures on a given field in the structures (in this case, R).
Can anyone shed some light on whether this is even possible, and if it turns out to be possible, point me towards a possible way of accomplishing it?
All your help is much appreciated.
Cheers, -KN
0 Comments
Accepted Answer
per isakson
on 23 Jun 2013
Edited: per isakson
on 23 Jun 2013
"Everything" is possible with Matlab, but not always in one line:-)
Since neither dat{:}.('R') nor dat{:}.R is not supported, I don't think sort a cell [array] of structures on a given field is possible with a couple of lines. However, try this
dat{1} = struct( 'R', [] );
dat{2} = struct( 'R', [] );
dat{3} = struct( 'R', [] );
for ii=1:3
dat{ii}.R = sprintf('#%d',rem(ii+1,3) );
end
for ii=1:3
names{ii} = dat{ii}.R;
end
[ ~, ix ] = sort( names );
dats = dat(ix);
dat{:}
dats{:}
it returns
ans =
R: '#2'
ans =
R: '#0'
ans =
R: '#1'
ans =
R: '#0'
ans =
R: '#1'
ans =
R: '#2'
0 Comments
More Answers (1)
See Also
Categories
Find more on Structures 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!