- 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
- 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.
- 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]
- 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.
Error using assignin in a nested m function
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Walter Roberson
2025년 11월 14일
testit()
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:
댓글 수: 0
추가 답변 (1개)
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
2025년 11월 14일
참고 항목
카테고리
Help Center 및 File 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!