MATLAB: Does evalin(…, assignin(…)) not work in MATLAB R2015b and more recent versions

15bassigninbasecallerchainevalinMATLAB

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?

Best Answer

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.