Generate variable names and assign them to workspace variables

13 views (last 30 days)
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

Accepted Answer

Iain
Iain on 12 Jul 2013
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} = .... ;

More Answers (1)

Image Analyst
Image Analyst on 12 Jul 2013

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!