Show Simulink simulation progess inside Matlab skript without too much performance loss.
20 views (last 30 days)
Show older comments
Hello Community
I have a large Simulnk model which takes quite long to run (+/- 30min).
Now I would like to set parameters and start the simulation using a m-file. If I do so, Simulink no longer shows me the progress of the simulation on the bottom.
I searched for solutions and I programmed a custom progress bar using the Matlab function waitbar() and an "Embedded Matlab Function" block inside simulink.
The code looks like this:
function fcn(u1, u2)
% u1 = Current simulation time
% u2 = Total run-time of the simulation
coder.extrinsic('waitbar');
coder.extrinsic('evalin');
coder.extrinsic('sprintf');
h = evalin('base','h');
perc = u1/u2 * 100;
waitbar(u1/u2, h, sprintf('Completed: %.05fs of %.05fs (%.05f%%)', u1, u2, perc)
Inside my Matlab skript I execute the simulation as follows:
h = waitbar(0,'Please wait...');
out = sim(...);
close(h);
This solution works but seems to slow down my simulation a lot, probably because this embedded function block is called within every simulation step. Also print functions like sprintf() are known to be not so fast.
Now there are several questions that come with this solution.
- I have not managed to "pass" my waitbar handler into this function block. That's why I am using the evalin() function in order to get it from the workspace. Is there a better way to do this?
- Is it possible to tell Simulink that it should only execute this embedded function block once in a while, for example, every 0.1s?
- I know that I can implement this functionality inside my Matlab skribt by using the get_param() function in order to get the current simulation time. However, I have not figured out how to run the sim() command in the "background" so that my Matlab skript continues and then can enter a loop in which the progress bar is updated. How can I implement that?
- Is there a general, better solution for this that offers more performance?
Thanks for any answers!
0 Comments
Answers (2)
Fangjun Jiang
on 30 Mar 2020
Add a Clock block to your root level model, or you can divide it by the totoal simulation time to get the percentage. You can use a Display block to just show the number or use a Scope block to show the curve. You can also set up sample time to mininize the efficiency impact. The value or curve will be updated when simulation is started by sim() command.
0 Comments
Dominik Hiltbrunner
on 31 Mar 2020
1 Comment
Fangjun Jiang
on 31 Mar 2020
I meant setting the sample time for the extra Clock, Display or Scope block to reduce its impact on your simulation.
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!