Why does evalin(..., assignin(...)) not work in MATLAB R2015b and more recent versions?

4 views (last 30 days)
From my function workspace, I want to get a variable from my base workspace and assign it to my current function workspace. In previous versions of MATLAB, I was able to do this using the command:
>> evalin('base','assignin(''caller'',''varName'',varName)');
However, in MATLAB R2015b and more recent versions, this command no longer works. It does not give any errors, but 'varName' is not created in the function workspace.
Why does "evalin" no longer work for this use case?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 May 2018
This is actually the expected behavior, as "evalin" and "assignin" cannot be used recursively to reach more than one level deep in the function call stack.
For more information, see the 'Limitation' section of the "evalin" documentation page:
This change was made in MATLAB R2015b (so R2015a and earlier versions have the old behavior you observed in R2014b) to standardize the behavior of "evalin".
As a workaround for MATALB R2015b and more recent versions, you can change the line
>> evalin('base','assignin(''caller'',''varName'',varName)');
to
>> varName = evalin('base', 'varName');
which will create a new variable, 'varName', in your function workspace with the same value as 'varName' in the base workspace.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!