Why does evalin(..., assignin(...)) not work in MATLAB R2015b and more recent versions?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 31 May 2018
Answered: MathWorks Support Team
on 31 May 2018
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
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!