For loop to make an array from workspace variables
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 13 Juin 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.
Réponses (4)
Steven Lord
le 13 Juin 2022
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.
0 commentaires
Jan
le 13 Juin 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 = ???
0 commentaires
Stephen23
le 13 Juin 2022
Modifié(e) : Stephen23
le 13 Juin 2022
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
0 commentaires
Dyuman Joshi
le 13 Juin 2022
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 commentaires
Dyuman Joshi
le 13 Juin 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.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!