Iterate through head structures using their names
Show older comments
Hi, I have 12 simulations that I've done and each has an associated structure with it. Each structure then has an x and y substructure. I'd like to iterate through the 12 structures and write the x substructure of each to a text file. Here is what I have:
simname = {'1','2','3','4A','4B','2S','3S','2t','3t','CC1','MC1','AS1'};
for ii = 1:12
sim = num2str(cell2mat(simname(ii)));
fid = fopen(strcat('sol',sim,'.txt','wt'));
ss = strcat('sol',sim);
fprintf(fid,'%12f \n',(ss).x); fclose(fid);
end
But it doesn't work because (ss).x is an "Unexpected MATLAB operator." I've iterated through substructures successfully with this kind of syntax before. Is there a way to iterate through the head structure names also?
thanks for any help, Sylvia
1 Comment
There is a way.... but you really don't want to use it. Just put all of your structures into one non-scalar structure or one cell array, and iterate over that.
What you call a "head structure" is actually just a variable: read my answer carefully to know why accessing lots of variable names dynamically is a buggy, slow, and obfuscated way to write code.
There is a huge difference between
(s).x
and
s.(x)
and you should not confuse them: the former does not exist (and is a very bad idea anyway), the latter is used to access fieldnames of a structure s (which is one variable).
Accepted Answer
More Answers (0)
Categories
Find more on Scripts 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!