MATLAB: Insert variable workspace in embedded MATLAB function

blockembedded matlab functionerrorloadsimulink

Hello, I find the following problem when I try to load a .mat file (containing parameters) into a simulink block (embedded MATLAB function). That is:
function y = fcn(u)
%#eml
load('var_mod');
if u <= L
y = go;
elseif u > L && u < L+s+(dp/2)
y = sqrt((u - L)^2 + go^2);
elseif u >= L+s+(dp/2) && u < L+d
y = (sqrt((-u + L + d)^2 + go^2));
else
y = go;
end;
Where L, d, go and so on should be read from the file var_mod.
The compilation gives the following error:
Function 'load' implicitly resolved in the MATLAB workspace. Implicit evaluation in MATLAB is not supported. Please declare this function extrinsic using eml.extrinsic('load'), or call it using feval.
Function 'Embedded MATLAB Function' (#18.27.42), line 4, column 1:
"load('var_mod')"
Launch diagnostic report.
How can I solve this?
Thanks

Best Answer

The load function is not amongst the functions supported for code-generation which cannot be used directly inside Embedded MATLAB Function blocks. You need to declare such functions as extrinsic functions to be able to do so.
However, instead of having to do this, I would recommend loading the file into the base-workspace (say, by using the LoadFcn callback of you model). Run the command:
>> set_param(modelname, 'LoadFcn', 'load('var_mod')');
>> set_param(modelname, 'CloseFcn', 'clear go;');
And then save your model. This sets up the model to load var_mod.mat every time it is opened and clear the variable "go" from the base workspace every time it is closed.
Then, use the Constant block and enter 'go' (without the single quotes) as the Value. connect the output of this block as a second input to your Embedded MATLAB block. You can introduce a second input by changing you function prototype to:
function y = fcn(u, go)
Now, you should be able to get rid of the call to "load" and use 'go' directly in your function.