Put variable names into a cell array or string array

조회 수: 7 (최근 30일)
dormant
dormant 2024년 10월 9일
이동: Rik 2024년 10월 12일
I have a number of similarly named variables in my script, eg
VarableAB
VarableDEF
VarableGHIJK
:
VarableZ
How can I get all these variable names into a cell array or a string array that I can loop round to do the same thing to each variable?
  댓글 수: 7
dormant
dormant 2024년 10월 10일
이동: Rik 2024년 10월 12일
I feel put-upon by this thread.
I am not a programmer, just a scientist who tries to use MATLAB to analyse data. Maybe I did paint myself into a corner with this problem, but I didn't know any better. I struggle with many concepts in MATLAB and other programming languages and don't feel that I have the time to learn them properly. Muddle through, that's my way.
Thanks for all the comments. I know you meant well.
Stephen23
Stephen23 2024년 10월 10일
이동: Rik 2024년 10월 12일
"I am not a programmer, just a scientist who tries to use MATLAB to analyse data."
Me too! That is, if mathematics counts as a type of science :)
"Maybe I did paint myself into a corner with this problem, but I didn't know any better."
We all start new topics knowing nothing about them.
"I struggle with many concepts in MATLAB and other programming languages and don't feel that I have the time to learn them properly."
Using vectors, matrices, arrays and indexing are not esoteric things that only programmers understand, they are really very fundamental to using MATLAB:

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

채택된 답변

dormant
dormant 2024년 10월 10일
I found I way to do what I want. Is this so bad?
S = whos( '-regexp', '^Varable' );
varables = extractfield( S, 'name' );
for i = 1:size(varables,2)
varable = char(varables(i));
myFunction( eval( varable ) );
end
  댓글 수: 1
Stephen23
Stephen23 2024년 10월 10일
편집: Stephen23 2024년 10월 10일
"Is this so bad?"
From the MATLAB documentation: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array. If the data sets are of different types or sizes, use a structure or cell array."

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

추가 답변 (1개)

Rik
Rik 2024년 10월 10일
Adding to the comments: you are putting data (a description and therefore a property) in the variable name. That is where your design is going wrong.
What you should probably have done is to write each of them to a separate mat file, or make them all fields of a struct.
When you have a bunch of mat files you can use dir to generate the array to loop over. If you choose to make it a struct array you can use fieldnames like this:
Varable.AB=1;
Varable.DEF=2;
Varable.GHIJK=3;
for fn=fieldnames(Varable).'
current = Varable.(fn{1});
fprintf('value of field %s is %d\n',fn{1},current)
end
value of field AB is 1 value of field DEF is 2 value of field GHIJK is 3
  댓글 수: 1
Stephen23
Stephen23 2024년 10월 10일
편집: Stephen23 2024년 10월 10일
Supplementing this answer: using a structure array and storing that meta-data in its own field also allows the use of meta-data which are not valid fieldnames:
S(1).data = 11;
S(1).meta = 'AB';
S(2).data = 23;
S(2).meta = 'CDE';
S(3).data = 99;
S(3).meta = 'This is not a valid fieldname';
for k = 1:numel(S)
fprintf('The value of element %d is %d and has text "%s"\n',k,S(k).data,S(k).meta);
end
The value of element 1 is 11 and has text "AB" The value of element 2 is 23 and has text "CDE" The value of element 3 is 99 and has text "This is not a valid fieldname"
and makes it easy to obtain data based on substrings on the meta-data:
iwant = 'valid'; % some substring
idx = contains({S.meta},iwant);
S(idx).data
ans = 99
Although rather than matching substrings you might as well store each meta-data value in its own field:
In short: better data design make your code more robust (as well as neater, more efficient, etc.).

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

카테고리

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

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by