MATLAB: Do I receive an error saying that a variable cannot be found when calling SAVE as an extrinsic function from Embedded MATLAB in Simulink 7.4 (R2009b)

simulink

I am attempting to execute a model containing an Embedded MATLAB Function block with the following code:
function save_input(u) %#eml
eml.extrinsic('save');
save('fromEML.txt','u','-ascii');
However, I receive the following error message:
??? Error using ==> save
Variable 'u' not found.

Best Answer

When a call to an extrinsic function is made from an Embedded MATLAB Block, the call is dispatched to MATLAB for execution. Therefore, the variables in the Embedded MATLAB execution environment are not directly available to the MATLAB environment. To enable the execution of the extrinsic function, Embedded MATLAB must transfer necessary variables to the MATLAB environment. In the invocation to SAVE, the input arguments are strings, and not variables. Hence, these strings are transferred to the MATLAB environment, but none of the local variables are.
In order to work around this situation, you must first marshal the required data over to MATLAB and then execute the extrinsic function.
For example:
function save_input(u) %#eml
eml.extrinsic('save','assignin');
assignin('base','u',u);
save('fromEML.txt','u','-ascii');