Matlab: "Error using assignin: Attempt to add "c"to a static workspace"

标签 matlab nested environment-variables symbolic-math

我有以下一段函数定义(测试代码):

function [X,Y,Z] = test(x,y,z)

syms a b c;
a = b + c;   % This is where it gets wrong

X=x;
Y=y;
Z=z;

keyboard

% nested functions
    function y = fun1(t,x)
        y=t+x;
    end

    function res = bvpbc(y0,yT)
       res= y0+yT;
    end

end

基本上,我在 test 函数中有一些嵌套函数,我在其中声明了一些符号变量 abc 。但是,当我通过键入运行函数时

测试(1,1,1)

总是有这样的错误信息:

Error using assignin
Attempt to add "b" to a static workspace.
 See MATLAB Programming, Restrictions on Assigning to
 Variables for details.

Error in syms (line 66)
        assignin('caller',x,sym(x));

Error in test (line 3)
    syms a b c;

似乎是符号声明有问题,但我不明白为什么。我该如何解决?

谢谢!

编辑:此外,每当我删除这两个嵌套函数时,test 函数都会正常工作。

最佳答案

以下最小工作示例重现了该问题,并且作为 Andrew Janke在评论中解释,不是错误:

function foo
syms A

    function nested
    end

end

您可以通过将符号变量显式分配给工作区来解决它:

A = sym('A');

关于Matlab: "Error using assignin: Attempt to add "c"to a static workspace",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17105493/

相关文章:

python - Matlab代码到Python代码的转换(使用数据帧)

ruby-on-rails - 在 Ruby 中查询 JSON 嵌套哈希响应时出现问题

java - 如何在 Java 上列出倒三角形上的数字

C++ - 在 Visual Studio 2010 的属性表中使用环境变量

heroku - 在 yarn package.json 中使用环境变量

linux - "env"和 "set"(在 Mac OS X 或 Linux 上)有什么区别?

xml - 如何将.mat文件转换为.xml文件?

matlab - 熔化 sample 的边缘检测和跟踪

java - 就像父类(super class)成员一样,父类(super class)构造函数是否构成子类对象状态的一部分?

arrays - 使用向量索引具有可变维数的元胞数组