Generate variable names and assign them to workspace variables
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello, I have data files in the workspace and I want to take parts of them and combine them in a certain order. the files are named according to a rule for example "p12_13_50" which means aerofoil 12 and 13 with 50% blending.
I want to combine the first, second and third column from 11 such files each into one variable.
I was able to generate the variable name using strcat as follows
af1 = 12;
af2 = 13;
polar_name = {};
for ii = 1:10:101
var_name = strcat('p',num2str(af1), '_',num2str(af2), '_', num2str(ii-1));
polar_name = [polar_name; var_name];
clear var_name
end
The trick is, how do I use the names that I have generated in calling part of the workspace variables with the same name. I have tried eval but it recognize the variable as a string. I also tried evalin and assignin without success.
If you think of other way to achieve the same goal I would be glad to try it as well.
Thanks in Advance
댓글 수: 0
채택된 답변
Iain
2013년 7월 12일
This is the answer to the question you've asked:
generated_variable_name = 'Big_whatever'; %(needs to be in your working workspace
eval([generated_variable_name ' = 5;'])
evalin('base',[generated_variable_name ' = 5;']);
assignin('base',generated_variable_name, 5)
The right answer is to use a structure or a cell array, as it is WAY more generic:
structure(2).filename = 'p12_13_50'
structure(2).aerofoils = [12 13];
structure(2).blending = 50;
structure(2).data = ...
cell{2}{1} = 'p12_13_50';
cell{2}{2} = [12 13];
cell{2}{3} = [50];
cell{2}{4} = .... ;
추가 답변 (1개)
Image Analyst
2013년 7월 12일
Don't do that. Please see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!