Matlab:“使用赋值的错误:尝试将c添加到静态工作空间”

时间:2021-11-18 12:07:40

I have the following piece of function definition (test code):

我有以下功能定义(测试代码):

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

Basically, I have some nested functions within the test function, where I declared some symbolic variables a, b and c. However, when I run the function by typing

基本上,我在测试函数中有一些嵌套函数,在这里我声明了一些符号变量a、b和c,但是,当我通过输入来运行函数时。

test(1,1,1)

测试(1,1,1)

there is always this error message:

总有这样的错误信息:

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;

It seems to be something wrong with the symbolic declarations, but I don't understand why. How can I fix it?

这种象征性的声明似乎有点问题,但我不明白为什么。我怎样才能修好它?

Thank you!

谢谢你!

EDIT: In addition, whenever I remove the two nested functions, the test function will work just fine.

编辑:此外,每当我移除两个嵌套函数时,测试函数就会正常工作。

1 个解决方案

#1


2  

The following minimal working example recreates the problem, and as Andrew Janke explains in the comments, is not a bug:

下面的最小工作示例重现了这个问题,正如Andrew Janke在评论中解释的那样,它不是一个bug:

function foo
syms A

    function nested
    end

end

you can work around it with an explicit assignment of the symbolic variable to the workspace:

您可以在其周围工作,将符号变量的显式赋值赋给工作区:

A = sym('A');

#1


2  

The following minimal working example recreates the problem, and as Andrew Janke explains in the comments, is not a bug:

下面的最小工作示例重现了这个问题,正如Andrew Janke在评论中解释的那样,它不是一个bug:

function foo
syms A

    function nested
    end

end

you can work around it with an explicit assignment of the symbolic variable to the workspace:

您可以在其周围工作,将符号变量的显式赋值赋给工作区:

A = sym('A');