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 件のコメント
Fadi Kahwash
Fadi Kahwash 2013 年 7 月 12 日
It's working fine, thanks for the prompt response...

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 7 月 12 日

カテゴリ

Help Center および File ExchangeIntroduction 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