For loop to make an array from workspace variables
显示 更早的评论
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 个评论
"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
2022-6-13
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
2 个投票
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
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 = ???
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 个评论
Yash
2022-6-13
Dyuman Joshi
2022-6-13
编辑:Dyuman Joshi
2022-6-13
You want to get the value of variables named as you mentioned? From workspace?
Yash
2022-6-13
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!