4

I am running Simulink using FastRestart, as I need to start and stop the simulation multiple times changing parameters. When I run Simulink from the main script, there are no problems. However, as soon as I make the script a function so that I can run it for different input data, I get an error that is clearly related to Simulink not seeing the Matlab workspace within the function.

To be more precise, say sfile is my Simulink file, then I run the following lines AFTER having initialized all variables I need in structures in Matlab:

load_system(sfile);
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');

At the last line, I get the error that Simulink does not recognize mdl.tStep (which is the time step), as mdl is not a recognized structure. In fact, it is and if I run Simulink from the main script everything is fine.

Now, in the past, I would have used

options = simset('SrcWorkspace','current');

However, an expert I know has advised me against simset (as it may get deprecated in the future) and encouraged me to use set_param instead. I have looked up the options for set_param on-line, but I could not find the setting for the Matlab workspace.

Any help would be greatly appreciated. Thank you in advance!

Enrico Anderlini
  • 447
  • 6
  • 21
  • 1
    Does this work: `options = sim(model, 'SrcWorkspace', 'current')` ? – Sardar Usama Mar 09 '17 at 14:34
  • Not really because before running `sim(sfile,...,options);` I am actually calling `set_param(sfile,'SimulationCommand','update');` – Enrico Anderlini Mar 09 '17 at 14:42
  • I know, it's not a very 'clean' solution, but you could try to use `assignin('base','mdl',mdl)` to get your variables into matlabs base workspace which simulink can read. – Max Mar 09 '17 at 17:12
  • Actually, that is exactly what I had done for all variables. Is there a way to specify the workspace through `assignin`, i.e. a way to specify `base`? – Enrico Anderlini Mar 09 '17 at 17:17
  • @EnricoAnderlini sorry, I don't understand your question. What do you want to specify? using `assignin` you can specify the workspace you want to assign the variables to. Assigning them to the base workspace should actually solve your problem – Max Mar 09 '17 at 17:22
  • Yes, after reading the help files for `assignin` I realise my question was wrong. Actually, the solution you suggest with `assignin` is what I tried and it works so if you post an answer, I will accept it. The `update` command still works in the simulation (when I start and stop it), so I do not have to repeat the commands of `assignin` multiple times after the first call, which is nice. – Enrico Anderlini Mar 09 '17 at 17:30

2 Answers2

4

In many instances it is better to use the Model Workspace rather than the Base Workspace:

hws = get_param(model, 'modelworkspace');
hws.assignin('mdl',mdl);

At least be aware that this option exists.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
2

A solution to your problem might be to use the assignin-function to all the variable whose value you want to pass to simulink in your matlab base workspace. To do so just use

assignin('base','mdl',mdl)

Having the variable in your base workspace should allow simulink to see it.

Max
  • 1,471
  • 15
  • 37