Programatically test-run a model before RTW build

15 views (last 30 days)
I am working on a script which sets up a few interfaces in a model and then builds it. Normally before running this script, I run my model for a few seconds to make sure there are no errors in the model itself. Since I am about to make this script available to a few others, I would like to automate this check. After searching here, I added a few variants of this code to my script:
try
eval([bdroot '([],[],[],''compile'')'])
SimRslt = get_param(ThisModel, 'SimulationStatus');
eval([bdroot '([],[],[],''term'')'])
catch
SimRslt = 'stopped';
end
if strcmp('stopped', SimRslt)
beep
msgbox(['Unable to compile this model due to a ' ...
'Simulink error. See the Matlab command window ' ...
'for information about the error.'], ...
'Model Error', 'error', 'modal')
return
end
This code caused two problems. First, my model ended up in strange locked-up states after running this script, and I had to type repeated commands to switch between "Compile" and "term" before it would free up.
Secondly, even after I removed this code, my model still seems somehow corrupted. Now after I build, the model's "SimulationStatus" is "stopped", but I still see "T=0.00" in the model's status bar as if its value would be "paused". To get out of this state, I have to send another "compile" command followed by a "term" command, because a "term" command alone gives me an error which says the model "must be compiled before it can be accessed programmatically". But how am I seeing "T=0.00" if it is not compiled?
I would appreciate any tips to fix my model, as well as suggestions for a better way to write my original script.

Accepted Answer

TAB
TAB on 17 Apr 2012
I am not sure why your model behaving like "PAUSED" even after executing "term" command. I can not see this behaviour on my models which i have tested.
Alternatively, to test you model is running or not, you can execute it by SimulationCommand
set_param(YourModel,'SimulationCommand','start') --> To start the simulation
set_param(YourModel,'SimulationCommand','stop') --> To stop the simulation
[Edited 12/4/2012]
Now I am able to reproduce the problem. This is very unexpected behaviour. 'compile' & 'term' commands works fine when issued from MATLAB command window, or when run step-by-step (using debugger) from m-file, but not works when whole m-script is exectued.
There is one way to make it work from m-file by introducing small delay after compilation and before terminate. I tried below code and it works.
mymodel([], [], [], 'compile') %Compile
pause(0.01); %Wait for 0.01 sec
mymodel([], [], [], 'term') %Ternminate

More Answers (4)

Kaustubha Govind
Kaustubha Govind on 17 Apr 2012
Perhaps you can tell us exactly what error your model runs into that reproduces this behavior with the model appearing to be "paused"? Like TAB, I am unable to reproduce the behavior with a simple test. Also, there may not always be an error reported in the MATLAB command window, so it might be a good idea to capture the exception like so:
try
eval([bdroot '([],[],[],''compile'')'])
SimRslt = get_param(ThisModel, 'SimulationStatus');
eval([bdroot '([],[],[],''term'')'])
catch err
errorMsg = err.message;
SimRslt = 'stopped';
end
if strcmp('stopped', SimRslt)
beep
msgbox(['Unable to compile this model due to a ' ...
'Simulink error: ' errorMsg], ...
'Model Error', 'error', 'modal')
return
end

Jeremy
Jeremy on 19 Apr 2012
Thank you both for your answers! I think I can now more clearly define the problem I am having with my model. If I type in my MatLab window to command the compile, wait for the compile command to complete, and then type in the "term" command, it all works. However, If I enter the "compile" command followed by the "term" command into an m-script, it creates the condition I tried to describe above. Some further programmatic accesses to the model gives me the error message "Error, [My Model] must be compiled before it can be accessed programmatically.". The only way I have found out of this state is to manually command a "compile" from the MatLab window, wait for it to complete, and then command a "term". I can find no way to exit this state through a script. Using the "SimulationCommand" alternative had the same results, with the same error message.
Is there any way I can cause my script to wait long enough between these commands? Something like:
eval([bdroot '([],[],[],''compile'')'])
while {Compile in process}
end
eval([bdroot '([],[],[],''term'')'])
Any other suggestions to stop the model from entering this mode, or to programmatically make it exit?
  2 Comments
TAB
TAB on 20 Apr 2012
Please see edited part in my answer.
Kaustubha Govind
Kaustubha Govind on 20 Apr 2012
What do you see in the command window when you run:
eval([bdroot '([],[],[],''compile'')']);get_param(bdroot, 'SimulationStatus'),eval([bdroot '([],[],[],''term'')']);

Sign in to comment.


Titus Edelhofer
Titus Edelhofer on 20 Apr 2012
Hi,
without solving the problem (I guess) I would suggest to modify the code to
feval(ThisModel, [], [], [], 'compile');
and
feval(ThisModel, [], [], [], 'term');
Interesting. I often use the same construct without problems so far. Did you try to add the pause statement? That indeed might help to flush queues ...
Titus
  2 Comments
TAB
TAB on 20 Apr 2012
Using pause() statement to introduce small delay works.
Titus Edelhofer
Titus Edelhofer on 20 Apr 2012
Hi TAB, Jeremy had not yet answered if it works also for him, that was the reason I asked ...

Sign in to comment.


Jeremy
Jeremy on 20 Apr 2012
I had been trying to figure out when it was done compiling and pause until that time. But then I saw the edits in TAB's first answer, and discovered that yes, any pause is enough to solve this problem. Thanks everyone for your assistance!

Categories

Find more on Interactive Model Editing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!