Iterate through head structures using their names

조회 수: 3 (최근 30일)
Sylvia Sullivan
Sylvia Sullivan 2016년 5월 11일
편집: Stephen23 2019년 6월 19일
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
Stephen23
Stephen23 2016년 5월 11일
편집: Stephen23 2016년 5월 12일
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).

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 5월 11일
편집: Stephen23 2019년 6월 19일
  댓글 수: 3
Sylvia Sullivan
Sylvia Sullivan 2016년 5월 11일
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 2016년 5월 11일
편집: Stephen23 2019년 6월 19일
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!:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scripts에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by