Generate variable names and assign them to workspace variables
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Iain
il 12 Lug 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} = .... ;
Più risposte (1)
Image Analyst
il 12 Lug 2013
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 Commenti
Vedere anche
Categorie
Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!