1
votes

I have a MATLAB function to solve a Inertia Tensor , and I have a nested function in my program . All the variables in it are symbolics but it told me

“Error using assignin: Attempt to add ”x“ to a static workspace”

and I don't understand why this happens . Here is my test.m code:


function test

syms x y z 
f=x
f1=f+1
f2=f1^2
    function r=test2        
        r=f2^3;
    end
f3=test2
end

After searching this web-forum I have found some answers . But at the same time I just don't understand it

Andrew Janke explianed it like this : While syms A may look like a static variable declaration, it isn't. It's just a regular function call. It's using Matlab's "command" invocation style to look like syntax, but it's really equivalent to syms('a', 'b', 'c'). on this page : Matlab: "Error using assignin: Attempt to add "c" to a static workspace"

what does static variable mean ?

I also search the HELP doc and it said :In functions and scripts, do not use syms to create symbolic variables with the same names as MATLAB® functions. For these names MATLAB does not create symbolic variables, but keeps the names assigned to the functions.

I only know syms x to create a symbolic variable in the workspace but why does the documentation say MATLAB does not create ?

3
If we attempt to dynamically add a variable to the workspace of an anonymous function, a nested function, or a function that contains a nested function, then MATLAB® issues an error of the form what does dynamically add a varibale mean ? - Hamburger

3 Answers

0
votes

'Static' means fixed, 'workspace' is what Matlab calls the places where all of its variables are stored. For non-nested functions the workspace starts off as empty when Matlab is at the beginning of the function; as Matlab continues through function's lines of code it continuously add more variables to the workspace.

For functions with a nested function, Matlab first parses the function to see what variable will be created (it specifically looks for x = type lines), then it creates all of these variables (with value as 'unassigned'), and then only does it start to run through the code; but while running through the code, it can never create a new variable.

This is why the code

function TestNestedFunction
syms x;
  function Nested()

  end
end

generates an error, there is no x = to tell it to pre-create the unassigned variable x at the start of the code. It fails at syms x;, as that line tries to create a new variable x, which fails as it may not.

This is also why the following code runs

function TestNestedFunction
syms x;
x = x;
  function Nested()

  end
end

it sees the x = and then pre-creates x. (This is why your example of adding [x, y, z] = deal([]); also works).

You can test this with a break point at the beginning of simple non-nested function and a simple nested function. Just run it step by step.

0
votes

This code works:

function test

x=sym('x')
y=sym('y')
z=sym('z')
f=x
f1=f+1
f2=f1^2
    function r=test2        
        r=f2^3;
    end
f3=test2
end
0
votes

I think the pages you found are quite clear. You need to declare the variables one by one and use:

x = sym('x') 

Otherwise syms will try to assign the values into a workspace where this is not allowed.