create a new variable whos name is conditional
3 次查看(过去 30 天)
显示 更早的评论
Hi. How can I creat a new variable whose name I dont know yet. A simple example is below. Thanks
>> [a,b,c]=xlsread('myfile.xlsx')
a =
[]
b =
'new_var'
c =
'new_var'
.... now I want to create a variable whose name is the same as the string taken from b . i.e I want to creat a variable with name new_var . and then give it a value ( new_var=10). How?
0 个评论
采纳的回答
Mischa Kim
2014-2-9
编辑:Mischa Kim
2014-2-9
mv = genvarname('my_varname');
eval([mv '= 123;']);
my_varname
3 个评论
Mischa Kim
2014-2-9
编辑:Mischa Kim
2014-2-9
Milad, it is pretty much the same. From your code, b is a string, just like
b = 'new_var'
mv = genvarname(b);
eval([mv '= 10;']);
new_var
In other words, you create (generate) a variable name with the string contained in b, then evaluate the command sequence [mv '= 10;'] which puts the newly generated variable with its content in the workspace.
更多回答(1 个)
Azzi Abdelmalek
2014-2-9
编辑:Azzi Abdelmalek
2014-2-9
Look at this link http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
You can do it like:
varname='new_var'
assignin('base',varname,[1 2 3])
new_var
But this is not a good idea like you can read it in the link
2 个评论
Azzi Abdelmalek
2014-2-9
I don't know the name of the variable
[a,b,c]=xlsread('myfile.xlsx')
assignin('base',b,[1 2 3])
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!