Matlab:“使用assignin错误:尝试添加”c“到静态工作空间“ [英] Matlab: "Error using assignin: Attempt to add "c" to a static workspace"
问题描述
我有以下功能定义(测试代码):
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
基本上,我有一些嵌套函数在测试
函数中,我声明了一些符号变量 a
, b
和 c
。但是,当我通过输入
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
test(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;
这似乎是符号声明有问题,但我不明白为什么。如何解决?
It seems to be something wrong with the symbolic declarations, but I don't understand why. How can I fix it?
谢谢!
编辑:另外每当我删除两个嵌套函数时,测试
函数将正常工作。
In addition, whenever I remove the two nested functions, the test
function will work just fine.
推荐答案
以下最小工作示例重新创建了问题,并且作为 Andrew Janke 在评论中解释,不是一个错误:
The following minimal working example recreates the problem, and as Andrew Janke explains in the comments, is not a 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');
这篇关于Matlab:“使用assignin错误:尝试添加”c“到静态工作空间“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!