Generate variable names and assign them to workspace variables

조회 수: 10 (최근 30일)
Fadi Kahwash
Fadi Kahwash 2013년 7월 12일
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

채택된 답변

Iain
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
Image Analyst 2013년 7월 12일

카테고리

Help CenterFile 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!

Translated by