Error using assignin in a nested m function

9 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 14 Nov 2025
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.

Più risposte (1)

Matt J
Matt J il 14 Nov 2025
Modificato: Matt J il 14 Nov 2025
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 Commento
Steven Lord
Steven Lord il 14 Nov 2025
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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by