Put variable names into a cell array or string array
Show older comments
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 Comments
Stephen23
on 9 Oct 2024
"I have a number of similarly named variables in my script..."
And that is the cause of your troubles. Solution: better data design.
dormant
on 9 Oct 2024
They aren't named with sequentially numbered names like x1, x2, x3, etc. but they are dynamic; they're not fixed when the code is written.
How did you create these variables? If you called load to bring them into the workspace, call it with an output argument so that all those struct arrays become fields in one variable whose name you know and have fixed when you wrote the code.
clear all
whos -file census.mat
data = load('census.mat') % variables in MAT-file become fields in struct
No matter what variables happen to be in census.mat, I can access them as fields of data. Dynamic field names aren't as problematic as dynamic variable names are.
fn = fieldnames(data)
x = data.(fn{1}) % the values from the variable cdate in census.mat
Note that I know exactly what variables will be in the workspace when those five lines of code are executed: data, fn, and x.
whos
What if census.mat had had variables named plot, min, max, quit, etc.? Would you expect MATLAB to access those variables or the functions with whom they share names?
Stephen23
on 10 Oct 2024
"I did it this way so that I can select one and pass it to a function for plotting or analysis."
Convenience for humans is only sometimes convenient for computer processing.
"The names were carefully chosen so that I could easly extract subsets by searching for substrings. (Maybe I should have given the real names instead of those made-up ones.)"
Variable names are not text, they cannot be searched for substrings.
If you had designed your data better (by storing that meta-data as data in its own right) then your task would be easy.
"Only now have I the need to do the same analysis on all 15 structures."
Yet you really painted yourself into corner with that bad data design. Solution: do not have 15 separate structures.
"I could easily cut and paste 15 function calls, but that approach is prone to errors and using a loop seemed better."
Loops are much better. Simple, robust, and efficient loops use indexing. To use indexing you need to have one array (not 15 separate structures). In your case, consider using a structure array and storing that meta-data (which is data after all) in its own field. Then you can easily do as much substring matching as you require (e.g. using comma-separated lists) and your code will be simple and neat and expandable and robust and efficient.
"I am not a programmer, just a scientist who tries to use MATLAB to analyse data."
Me too.
"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:
Accepted Answer
More Answers (1)
dormant
on 10 Oct 2024
1 Comment
"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."
Categories
Find more on Data Type Identification 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!