Programatically test-run a model before RTW build
6 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
0 Comments
More Answers (4)
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
0 Comments
Jeremy
on 19 Apr 2012
2 Comments
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'')']);
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
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 ...
See Also
Categories
Find more on Simulink Functions 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!