Programatically test-run a model before RTW build
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
TAB
2012-4-17
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 个评论
更多回答(4 个)
Kaustubha Govind
2012-4-17
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 个评论
Jeremy
2012-4-19
2 个评论
Kaustubha Govind
2012-4-20
What do you see in the command window when you run:
eval([bdroot '([],[],[],''compile'')']);get_param(bdroot, 'SimulationStatus'),eval([bdroot '([],[],[],''term'')']);
Titus Edelhofer
2012-4-20
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 个评论
Titus Edelhofer
2012-4-20
Hi TAB, Jeremy had not yet answered if it works also for him, that was the reason I asked ...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Model Editing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!