How to compile Simulink models before doing simulation?

270 Ansichten (letzte 30 Tage)
Rednar
Rednar am 12 Jul. 2013
Beantwortet: Brian Kim am 19 Mai 2023
I'm working on a MATLAB script that will run different Simulink models but everytime I load one using sim(myModel) it takes an awful lot of time and I need its execution to be practically immediate.
Is there a way to compile at the start of the script and later on when I need to run the simulation just tell it 'play' somehow so that it will run without taking time to load anything else? Or is there any other workaround for this problem?
By the way I tried using
open_system(myModel)
and it gives me an error: Invalid Simulink object handle
Unfortunately that's all it says, but if that's the answer to compiling before the simulation, any ideas of what the problem could be?
Thanks.
  1 Kommentar
Kaustubha Govind
Kaustubha Govind am 12 Jul. 2013
Try open_system('myModel') if myModel is the name of your model. This just opens the model, but doesn't compile it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 12 Jul. 2013
You can use the modelname command to compile the model. For example, if your model is named myModel.mdl or myModel.slx, use the following set of commands:
myModel([], [], [], 'compile')
myModel([], [], [], 'term')
Note that Simulink will still need to perform some compilation tasks when you run the simulation, but if you have Stateflow charts, MATLAB Function blocks, or referenced models, it will definitely help to pre-compile your model.
  7 Kommentare
Andrew Hopkins
Andrew Hopkins am 14 Jun. 2018
Is there any documentation on executing the compiled model?
output_vector = model_name(0,state_vector,input_vector);
I have not been able to find any documentation on this, especially the structure of the state, input, output, and what the first argument corresponds to.
Charles Harrison indicated this can be done:
https://www.mathworks.com/matlabcentral/answers/20730-supress-simulink-re-compile-on-every-call
Andrew Hopkins
Andrew Hopkins am 14 Jun. 2018
It's sad how much I searched, and then a coworker found this link in 5 seconds.
It was not clear from the other posts (or even the model documentation), that you need to call each function successively for each time step for some total amount of simulation time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 12 Jul. 2013
  1 Kommentar
Rednar
Rednar am 12 Jul. 2013
My simulation is fine and the problem with the time it takes to compile is that I use complex blocks for brain signal recording and bandpower extraction, but I don't mind the time it takes to compile, I'm just wondering if there's a way to do that compilation of the model beforehand and then run the simulation immediately when I command it?

Melden Sie sich an, um zu kommentieren.


Brian Kim
Brian Kim am 19 Mai 2023
You can use also use set_param as well to compile the model
set_param(modelName, 'SimulationCommand', 'update')
The difference is that in scripting workflow, this will wait until the compilation is finished.

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by