For loop to make an array from workspace variables

36 visualizaciones (últimos 30 días)
Yash
Yash el 13 de Jun. de 2022
Editada: Stephen23 el 13 de Jun. de 2022
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 comentarios
Yash
Yash el 13 de Jun. de 2022
The variables are coming from a .mat file that is converted from a data log. So one option of changing the naming convention of the imported variables would be renaming them after loading the .mat into matlab.
Stephen23
Stephen23 el 13 de Jun. de 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.

Iniciar sesión para comentar.

Respuestas (4)

Steven Lord
Steven Lord el 13 de Jun. de 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.

Jan
Jan el 13 de Jun. de 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 = ???

Stephen23
Stephen23 el 13 de Jun. de 2022
Editada: Stephen23 el 13 de Jun. de 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

Dyuman Joshi
Dyuman Joshi el 13 de Jun. de 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
xlength = 32×25 char array
'Object_00_number_00_Right' 'Object_01_number_01_Right' 'Object_02_number_02_Right' 'Object_03_number_03_Right' 'Object_04_number_04_Right' 'Object_05_number_05_Right' 'Object_06_number_06_Right' 'Object_07_number_07_Right' 'Object_08_number_08_Right' 'Object_09_number_09_Right' 'Object_10_number_10_Right' 'Object_11_number_11_Right' 'Object_12_number_12_Right' 'Object_13_number_13_Right' 'Object_14_number_14_Right' 'Object_15_number_15_Right' 'Object_16_number_16_Right' 'Object_17_number_17_Right' 'Object_18_number_18_Right' 'Object_19_number_19_Right' 'Object_20_number_20_Right' 'Object_21_number_21_Right' 'Object_22_number_22_Right' 'Object_23_number_23_Right' 'Object_24_number_24_Right' 'Object_25_number_25_Right' 'Object_26_number_26_Right' 'Object_27_number_27_Right' 'Object_28_number_28_Right' 'Object_29_number_29_Right' 'Object_30_number_30_Right' 'Object_31_number_31_Right'
%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
y = 32×1 cell array
{'Object_00_number_00_Right'} {'Object_01_number_01_Right'} {'Object_02_number_02_Right'} {'Object_03_number_03_Right'} {'Object_04_number_04_Right'} {'Object_05_number_05_Right'} {'Object_06_number_06_Right'} {'Object_07_number_07_Right'} {'Object_08_number_08_Right'} {'Object_09_number_09_Right'} {'Object_10_number_10_Right'} {'Object_11_number_11_Right'} {'Object_12_number_12_Right'} {'Object_13_number_13_Right'} {'Object_14_number_14_Right'} {'Object_15_number_15_Right'} {'Object_16_number_16_Right'} {'Object_17_number_17_Right'} {'Object_18_number_18_Right'} {'Object_19_number_19_Right'} {'Object_20_number_20_Right'} {'Object_21_number_21_Right'} {'Object_22_number_22_Right'} {'Object_23_number_23_Right'} {'Object_24_number_24_Right'} {'Object_25_number_25_Right'} {'Object_26_number_26_Right'} {'Object_27_number_27_Right'} {'Object_28_number_28_Right'} {'Object_29_number_29_Right'}
  4 comentarios
Yash
Yash el 13 de Jun. de 2022
Yes, essentially I want the array that is being created to have the values of all the variables rather than just the variable name. Ibelieve I have to utilize the eval (which is typically discouraged) command but I'm not sure how to go about it.
Dyuman Joshi
Dyuman Joshi el 13 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by