Search code examples
matlabfunctionsimulinkworkspace

Putting variables from Function workspace to base workspace


I have a script calling a function, calling a function to simulate a model. The 2nd function also loads variables that the simulink model Needs for lookuptables. Now if I run it, matlab tells me, it doesn't know those variables. So I assume, that the variables have to be in base-workspace for simulink to recognise them and I tried to call

vars=whos;
for k_laden=1:length(vars)
assignin('base', vars(k_laden).name, eval(vars(k_laden).name))
end

in the 2nd function, but then matlab tells me

??? The variable "ans" is undefined.

Error in ==> esm6 at 76 assignin('base', vars(k_laden).name, eval(vars(k_laden).name))

Error in ==> Optimierung_Querdynamik_MBT at 87 y = esm6(param_komplett,identi);

Error in ==> Identi at 176 [param_plotten , guete_wert] = Optimierung_Querdynamik_MBT(ESM_Wahl,Plotten,param_plotten,identi);

Does anyone know how to solve this issue? Thanks in advance.

EDIT: the variables that simulink Needs, are created by the load-command. So it might help if you give me a way to call the load command from a function but directly loading the variables into base-workspace.
Also it might help, if i could define which Workspace the simulink model is using.


Solution

  • By default Simulink looks in the base workspace, but assuming that you are simulating the model by using the sim function within your function, then just define the variables in the functions workspace and use

    sim('modelname','SrcWorkspace','current');