MATLAB: How to compile Simulink models before doing simulation

MATLABscriptsimulationsimulink

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.

Best Answer

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.