For loop to make an array from workspace variables
Mostra commenti meno recenti
I am new to programming and wanted to know the best approach for creating an array with needed signals that are imported into the matlab workspace. For instance the variables are named something like:
Object_01_number_01_Right
Object_01_number_02_Right...uptill object 32
And the same with the left side:
Object_01_number_01_Left
I am trying to make an array for each, left and right side but am not sure what the best approach is. Manually making the array is an option, but it seems inefficient for 32 objects. I am trying something like:
xlength=[];
for k=0:31
xlength=['Object_' num2str(k) 'number_' num2str(k) '_Right'];
end
The problem with this is that, my loop is only keeping the last value, and the array created is of characters and not the data within the matlab variable because I am using the num2str command. Is there another command I should be using? and what change needs to be made to ensure all 32 signals are in the array?
3 Commenti
"I am new to programming and wanted to know the best approach for creating an array with needed signals that are imported into the matlab workspace."
Best approach: do NOT hide meta-data (such as indices) in variable names.
So far you did not tell us the most important information: how did you get all of those variables into the workspace?
Fixing the problem at the source would be much better than messing around with dynamic variable name later.
Yash
il 13 Giu 2022
Stephen23
il 13 Giu 2022
"So one option of changing the naming convention of the imported variables would be renaming them after loading the .mat into matlab."
Even better, LOAD into an output variable and avoid this whole mess.
Risposte (4)
Steven Lord
il 13 Giu 2022
2 voti
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
Jan
il 13 Giu 2022
"the variables are named something like: Object_01_number_01_Right"
This is the core of the actual problem. Hiding data in the names of variables makes it very complicated to access them. You need a kind of meta programming, e.g. by eval: code, which creates code to be executed. Don't do this. See: TUTORIAL: why and how to avoid Eval .
A solution: Use arrays:
Object(1).number = 1;
Object(1).side = 'Right';
Object(1).value = ???
Much much better approach: rather than messing about with ugly EVAL, you should always LOAD() into an output variable (which is a scalar structure containing the imported data):
S = load(..)
and then access its fields. You will find FIELDNAMES() and dynamic fieldnames very useful for this:
or even functions like STRUCT2CELL() or STRUCT2TABLE() or similar. Result: simpler more efficient code.
"I am new to programming .."
Tip1: Always LOAD() into an output variable.
Tip2: When you design data in future, do not force meta-data into variable names. Doing so makes processing the data harder: https://www.mathworks.com/matlabcentral/answers/225435-save-variable-as-string-from-user-input#answer_184104
The way you have written your loop, the variable xlength gets overwritten every iteration.
%Concatenating in a char array
xlength=[];
for k=0:31
xlength=[xlength;'Object_' sprintf('%02d', k) '_number_' sprintf('%02d', k) '_Right'];
end
xlength
%You can also store them in a cell array
y={};
for k=0:31
y{k+1,1}=['Object_' sprintf('%02d', k) '_number_' sprintf('%02d', k) '_Right'];
end
y
4 Commenti
Yash
il 13 Giu 2022
Dyuman Joshi
il 13 Giu 2022
Modificato: Dyuman Joshi
il 13 Giu 2022
You want to get the value of variables named as you mentioned? From workspace?
Yash
il 13 Giu 2022
Dyuman Joshi
il 13 Giu 2022
As @Steven Lord and @Jan pointed, creating dynamic variables is not recommended. Refer to the link Steven posted.
Use numeric/cell array to define/load your variables into. You can look into struct as well, as Jan commented.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!