Create variable with name from other variable string

49 次查看(过去 30 天)
Hello,
My question is, if i have a string variable, coulf i assing that string as a name for a new variable?
For example, if i have
name_string = 'name_var';
can i have
name_var = 5;
somehow wihouth me writing the actual code?

采纳的回答

Joseph Cheng
Joseph Cheng 2021-6-26
While it is not adviseable to have variables created in this way maybe you have a good reason to but still un-adviseable but here you go:
name_str = 'name_var';
eval(sprintf('%s = 5',name_str))
name_var = 5
disp([name_str 'has been assigned to ' num2str(name_var)])
name_varhas been assigned to 5
  3 个评论
Victor Andrés Sánchez Zurita
Hello!! Thank you very much for the answer,
Of course I can give the simplified context,
I have an excel file with some names and some related number codes, i will read the excel list and do some math operations with the code and then generate a "NAME_CODE.mat" file so i can email each pearson their files, no one can have access to another person file.
Stephen23
Stephen23 2021-6-28
编辑:Stephen23 2021-6-28
@Victor Andrés Sánchez Zurita: nothing in your description requires dynamically named variables.
Most likely this is a complex and inefficient approach to processing your data.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by