For loop to make an array from workspace variables
27 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-6-13
"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.
回答(4 个)
Steven Lord
2022-6-13
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 个评论
Jan
2022-6-13
"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 个评论
Stephen23
2022-6-13
编辑:Stephen23
2022-6-13
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 个评论
Dyuman Joshi
2022-6-13
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 个评论
Dyuman Joshi
2022-6-13
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!