Running Simulink from within a Matlab function

4 visualizaciones (últimos 30 días)
Enrico Anderlini
Enrico Anderlini el 9 de Mzo. de 2017
Comentada: Enrico Anderlini el 15 de Mzo. de 2017
I was running Simulink from the main script with FastRestart and avoiding the use of simset and all was good.
However, after enclosing the script in a function so that I can call it from a different script for different input data, I now get an error related to the workspace. Basically, Simulink is no longer able to access the workspace within the function.
I have found solutions on-line using simset, but this is not an option in my case. Assuming sfile is my Simulink file, I first load the system
load_system(sfile);
and then set-up the FastRestart settings
set_param(sfile,'FastRestart','on');
set_param(sfile,'SaveFinalState','on');
set_param(sfile,'SaveCompleteFinalSimState','on');
set_param(sfile,'SimulationCommand','update');
Now, the error occurs on this last line, i.e. when I am updating the Simulink file. Basically, Simulink returns an error because it does not recognize mdl.tStep, i.e. the step size length, as a structure because it is not specified. In fact, it is because it is in the workspace.
So, my question is how I can sort out this problem. Is it possible to use set_param to change the workspace now that simset is deprecated? Or should I rather set all variables through set_param individually?

Respuestas (1)

Brandon Eidson
Brandon Eidson el 15 de Mzo. de 2017
Hey Enrico, the reason you are getting an error now that you have enclosed the script in a function is because a function has its own workspace separate from the base workspace. You can read more about this distinction in the documentation linked to below.
Because you call the model from the function, the model only has access to the function workspace. If the simulation depends on any user-defined variables, such as your "tStep", and you want to update or simulation the model from a function, you will need to pass those user-defined variables to the function as arguments.
  1 comentario
Enrico Anderlini
Enrico Anderlini el 15 de Mzo. de 2017
Thanks Brandon. You are absolutely right. I sorted out the problem by assigning all variables with the assignin command. Not a very neat solution, but effective nonetheless.

Iniciar sesión para comentar.

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by