Programatically test-run a model before RTW build
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
TAB
el 17 de Abr. de 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 comentarios
Más respuestas (4)
Kaustubha Govind
el 17 de Abr. de 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 comentarios
Jeremy
el 19 de Abr. de 2012
2 comentarios
Kaustubha Govind
el 20 de Abr. de 2012
What do you see in the command window when you run:
eval([bdroot '([],[],[],''compile'')']);get_param(bdroot, 'SimulationStatus'),eval([bdroot '([],[],[],''term'')']);
Titus Edelhofer
el 20 de Abr. de 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 comentarios
Titus Edelhofer
el 20 de Abr. de 2012
Hi TAB, Jeremy had not yet answered if it works also for him, that was the reason I asked ...
Ver también
Categorías
Más información sobre Interactive Model Editing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!