How to compile Simulink models before doing simulation?

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

Try open_system('myModel') if myModel is the name of your model. This just opens the model, but doesn't compile it.

댓글을 달려면 로그인하십시오.

 채택된 답변

Kaustubha Govind
Kaustubha Govind 2013년 7월 12일
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.

댓글 수: 9

That's awesome, just what I need, I think, but is there a way to do that pre-compilation and then run the simulation normally instead of step by step? I'm guessing that's what Simulink Coder is for isn't it?
I just figured a workaround, I put the simulation to run during Inf seconds, pause it and then resume it whenever I need to, cheesy but gets the job done. Your answer was close to what I was looking for tho, thanks.
That's great but how to we do when the model name is unkown before execution ?
I can't do wModelName([],[],[],'compile')
Nikhil
Nikhil 2016년 3월 4일
편집: Nikhil 2016년 3월 4일
@Zangdaarr: You could try using the eval function
Cmd=[wModelName, ' ([],[],[], ' 'compile' ' ); ' ];
eval(Cmd);
Works perfect ...just one correction
>>compile='compile'; >>Cmd=[wModelName, ' ([],[],[], ' 'compile' ' ); ' ]; >>eval(Cmd)
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
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.
when trying this approach I found that it doe not work for models with Accelerator mode. It reports
'Accelerator mode is not supported through the model command.'
Any suggestion how to solve this?
--Han
Hint, as I struggled a while on this:
If the name of your model is stored in a variable e.g. testMdlName, then you can not execute
testMdlName([], [], [], 'compile')
I got the error: Index in position 4 exceeds array bounds. Index must not exceed 1.
But you can do this:
eval( [testMdlName '([], [], [], ''compile'')'] )

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Brian Kim
Brian Kim 2023년 5월 19일
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.

댓글 수: 1

When I use that command I get the build text through the command window and eventually get the error:
Warning: Non-scalar data in first argument to set_param is detected.
Only first handle in the matrix is processed.
Invalid Simulink object handle
However, when I run:
set_param('modelName','SimulationCommand', 'update')
it runs the command and does not produce any comments in the command window.
Two questions:
  1. do you know why running the set_param() command with my modelName NOT in quotes does what I want it to do but throws the invalid simulink object handle?
  2. Is there a way to get the output of set_param() with my ModelName in quotes to produce an output in the command window?
Thanks for your help!

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 12일

0 개 추천

댓글 수: 1

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?

댓글을 달려면 로그인하십시오.

Abhisar
Abhisar 2025년 7월 5일

0 개 추천

There's a lot easier way to do this.
Just turn on fast restart in the SIMULATE tab in your simulink model. Now, whenever you call simOut, the model will not compile everytime, so you can change a variable from your matlab scipt and run the model iteratively, way faster than before.

카테고리

도움말 센터File Exchange에서 Prepare Model Inputs and Outputs에 대해 자세히 알아보기

질문:

2013년 7월 12일

답변:

2025년 7월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by