Error using assignin in a nested m function

조회 수: 9 (최근 30일)
feynman feynman
feynman feynman 2025년 11월 14일
편집: Matt J 2025년 11월 14일
Error using assignin
Attempt to add "q" to a static workspace.
Error in syms (line 283)
assignin('caller', x, xsym);
Error in mywork/myfunction (line 66)
syms a(t) q
This error occurs when running mywork.m but not myfunction alone.

채택된 답변

Walter Roberson
Walter Roberson 2025년 11월 14일
testit()
Error using assignin
Attempt to add "q" to a static workspace.

Error in syms (line 262)
assignin('caller', x, xsym);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in solution>testit/inner (line 6)
syms q
^^^^^^
Error in solution>testit (line 4)
inner()
^^^^^^^
function testit
inner()
function inner
syms q
end
end
When you have nested functions, the workspace of the inner function is considered to be "static". You cannot use evalin() or assignin() or the like to add new variables to static workspaces: every variable assignment into the workspace must be explicit using = . It happens that syms uses assignin to assign variables, which conflicts with the explicit assignment needed for static workspaces.
You have a few choices:
  1. You can change syms a(t) q to be t = sym('t'); q = sym('q'); a = symfun('a(t)', t); . This creates local variables using equivalent functionality to syms a(t) q . Note that it gets messier to replicate all of syms using just sym -- for example syms a(t) [3 4] gets kind of messy. If I recall correctly, there are some uncommon uses of syms that cannot be replaced by using sym
  2. You can use a = []; q = []; t = []; syms a(t) q . This creates local variables that are then overwritten using syms . Note that it gets messier to replicate all of syms using this method; for example syms a(t) [3 4] would require initializing a number of variables first.
  3. You can assign something to a and q and t in the outer function, before defining the inner function, and continue to assign using syms a(t) q in the inner function. That would make a q t into shared variables, which can be assigned to by assignin(). These are not local variables. Note that it gets messy to inintialize all variables for something like syms a(t) [3 4]
  4. In this particular case, you could use syms a(t) q in the outer function, before defining the inner function, which will create shared variables that will be inherited by the inner function. This would not create local variables.

추가 답변 (1개)

Matt J
Matt J 2025년 11월 14일
편집: Matt J 2025년 11월 14일
Assignin is something you should generally try to avoid. It has many hazards.
In this case, where myfunction is nested in mywork, it should be especially unnecessary. Just pre-initiliaze q in mywork's workspace (e.g. set it to empty, []). Then, q will automatically be shared with myfunction's workspace as well, see Sharing Variables Between Parent and Nested Functions.
  댓글 수: 1
Steven Lord
Steven Lord 2025년 11월 14일
If you're trying to define symbolic variables in a nested function, you should use the sym function with an output argument instead of using the syms function.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by