Iterate through head structures using their names

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Stephen23
Stephen23 il 11 Mag 2016
Modificato: Stephen23 il 12 Mag 2016
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).

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 11 Mag 2016
Modificato: Stephen23 il 19 Giu 2019
  3 Commenti
Sylvia Sullivan
Sylvia Sullivan il 11 Mag 2016
Actually, Stephen, could you recommend any general books - rather than links - that are good for understanding "best programming practices" (for MATLAB or in general) like not dynamically naming variables? It would be nice, in general, to have a cohesive guide in thinking about more elegant solutions.
Stephen23
Stephen23 il 11 Mag 2016
Modificato: Stephen23 il 19 Giu 2019
I would love to help, but really I don't know any MATLAB books... I learned to code several languages by taking online courses or tutorials, and then doing lots and lots of reading online.
Start by reading these points carefully, as something to think about (I know it is a link!, please trust me):
I think it would help to split your task into two:
  • Good code practices (apply to any language)
  • Good MATLAB practices (specific to MATLAB)
The former you can learn anywhere, and is going to be the same: document your code, write comments, use test cases, etc, etc. The latter... well... one of the best ways to learn how to use MATLAB properly is... the MATLAB documentation: it is easy to read, it has articles and examples, and is full of useful advice. Never simply take someone's word with regard to MATLAB: it is very easy to check what the documentation explains. Any internet search engine will help you :)
I am not sure why you prefer books, but a lot of information is online too: try using [your favorite search engine] and the terms "MATLAB best practice" or "MATLAB guide" or similar. In my experience no single third-party source was always good at explaining everything, nor was every third-party source always correct, so you should read plenty of sources then you will soon get a feeling for the common advice, best practice, and reasons for them.
Note that books may not represent the current status of MATLAB, nor even the version that you use.
Not books, but worth a read anyway:
Oh, and a book!:

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Scripts in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by