在Matlab R2013a中使用eval进行动态变量分配 [英] Dynamic variable assignment using eval in Matlab R2013a

查看:74
本文介绍了在Matlab R2013a中使用eval进行动态变量分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Matlab中使用 eval 将值分配给动态变量.我正在使用Matlab R2013a.我可以轻松地在旧版本中进行操作,但在R2013a中则无法.是的,我知道使用 eval 等的陷阱.我也尝试过 assignin ,但这并不成功.

I am trying to assign values to a dynamic variable using eval in Matlab. I'm using Matlab R2013a. I can easily do it older versions but not in R2013a. Yes, I'm aware of the pitfalls of using eval, etc. I tried assignin as well, but that wasn't successful.

Bins = [10 100 20]; 
Cols = numel(Bins);

for i = 1:Cols 
    eval(['Var' num2str(i) ' = Cols+i']);
end

错误->尝试将"Var"添加到静态工作区.

Error --> Attempt to add "Var" to a static workspace.

所需的输出

    Var1 = 4 
    Var2 = 5
    Var3 = 6

推荐答案

该代码是否在嵌套函数内部?请参阅《 本文MathWorks.

Is that code inside of a nested function? See this article from The MathWorks.

如果必须以这种方式创建变量(有更好的选择),则需要在主函数中或使用子函数来完成.请参阅此有关在工作空间之间共享数据的文章,有关不同类型的功能以及如何使用它们的详细信息.

If you must create variables that way (there are much better options) then you'll need to do it within the main function or use a sub-function. See this article on sharing data between workspaces for details on the different types of functions and how to use them.

这篇关于在Matlab R2013a中使用eval进行动态变量分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆