You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
pause simulation and do calculation
5 views (last 30 days)
Show older comments
Hi,
I have a unique need to pause simulation at known time points and do calculation with the obtained workspace data to that point, plot it, then resume simulation. What is the best way of setting it up with a subsystem and its InitFcn, or other callback functions?
Thanks
Accepted Answer
Ameer Hamza
on 23 Apr 2020
See breakpoints in MATLAB: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html
21 Comments
Ameer Hamza
on 23 Apr 2020
If you are using a recent version of MATLAB (R2019a forward), then in Simulink, you can go to the debug tab and specify the pause time. If you are using an older release, then you can use the assertion as described here: https://www.mathworks.com/help/simulink/ug/controlling-execution-of-a-simulation.html#bq0ht63
Golden
on 23 Apr 2020
it helps, but I encounter another problem with it. see the attached example please in 2017a.
In the pauseFcn, I added these two lines, but the simulation does not resume. why?
set_param([gcb,'/Constant1'],'Value','2');
set_param(bdroot,'SimulationCommand','continue');
Thanks,
Ameer Hamza
on 24 Apr 2020
Where have you added these lines?
Ameer Hamza
on 24 Apr 2020
Gloden, first, the value of the constant block is set to 1 and, therefore, the simulation pauses at 1 second. It also sets the value of constant block to 2. If you press continue, the simulation will again pause at 2 seconds, and the value of constant block remains the same. Now, if you press continue again since the constant block is still 2, the assertion becomes false, and the simulation does not resume.
Golden
on 24 Apr 2020
Thanks Ameer for looking into it.
I guess I should be more clear: I would think I would not need to "press continue" when it first pauses because I have a line in pauseFcn:
set_param(bdroot,'SimulationCommand','continue');
That was my expection anyway. I basically would like to be automatic resuming without user intervention. Any idea to do it?
Ameer Hamza
on 24 Apr 2020
Can you explain the scenario? In the question you mentioned about pausing and checking the values. But why do you want to pause and automatically resume. Maybe there is some other way, if you can describe the actual purpose.
Golden
on 24 Apr 2020
Sure. I would like to dynamically update a figure where I can plot data obtained from the partial simulation. Say, I would like to use the plot function to plot y vs t in the example. Since y and t are in the workspace, I'd think I should be able to do it as long as the simulation is paused. After the figure is updated in the pauseFcn callback, which is my plan anyway until you give me a better idea, the simulation can resume until it hits next pause point. I like this process of pausing, updating figure, and resuming to be automatic so user just see the figure being updated as time goes on without much intervention. Thanks.
Ameer Hamza
on 24 Apr 2020
If you want to visualize simulation data using MATLAB figures, then the event listener is a much better option. See the examples here: https://blogs.mathworks.com/simulink/2013/01/25/accessing-block-data-during-a-simulation/ and here: https://www.mathworks.com/matlabcentral/fileexchange/24294-simulink-signal-viewing-using-event-listeners-and-a-matlab-ui
Golden
on 24 Apr 2020
Thanks for the suggestion. It will take me some time to absorb it. Do you have any idea what command I should use so that the simulation will resume after the pause? I have a feeling I am not using the line correctly:
set_param(bdroot,'SimulationCommand','continue');
Either it was because the assertion needs to be reset somehow or it is not the right command to use. Does this make sense?
Ameer Hamza
on 24 Apr 2020
It is the correct command, but it seems that the parameter 'SimulationCommand' cannot be set to 'continue' during the execution of pauseFcn. As a workaround, I created a timer(), which is able to run a function independent of the pauseFcn. Add the following line in the pauseFcn
set_param([gcb,'/Constant1'],'Value','2');
t = timer('ExecutionMode', 'singleShot', ...
'StartDelay', 0.1, ...
'TimerFcn', @timerCB);
start(t);
and define the following function and place it the file named timerCB.n in the MATLAB path.
function timerCB(obj,~)
set_param(bdroot,'SimulationCommand','continue');
stop(obj);
delete(obj);
end
Golden
on 24 Apr 2020
It works. Thank you very much. If you can think of any other workaround that does not involve adding this timerCB.m file please let me know. I really would like my subsystem to be standalone block so people use it need not to do anything else including adding a file to MATLAB path. Thanks again.
Ameer Hamza
on 24 Apr 2020
Ok. Here is a stand-alone solution. The only side-effect is that it needs you to create a global variable. Choose a complicated name such that it does not conflict if the user has defined a global variable with the same name. Remember to change the all occurance of variable T in this code.
set_param([gcb,'/Constant1'],'Value','2');
global T % use a complicated name so that it does not
% conflict with the user-defined global variable name
T = timer('ExecutionMode', 'singleShot', ...
'StartDelay', 0.1, ...
'TimerFcn', "set_param(bdroot,'SimulationCommand','continue'); stop(T); delete(T);");
start(T);
Golden
on 24 Apr 2020
it would be a good solution, but i got this:
An error occurred while running the simulation and the simulation was terminated
Caused by:
Error evaluating 'PauseFcn' callback of SubSystem block 'resuming_test/VB'.
TimerFcn callback must be set to a string, a function handle, or a
1-by-N cell array. The first element of the 1-by-N cell array must be
the callback function name or handle.
i'm using 2017a. I wonder if this is the problem. Thanks.
Ameer Hamza
on 24 Apr 2020
Strings were introduced in R2016b, so they might not be fully supported for callback declaration in R2017a. Try the following version using a character array
T = timer('ExecutionMode', 'singleShot', ...
'StartDelay', 0.1, ...
'TimerFcn', 'set_param(bdroot,''SimulationCommand'',''continue''); stop(T); delete(T);');
Note that in the above statement, only single quotation marks (') are used. There is no double quotation mark.
Ameer Hamza
on 25 Apr 2020
Can you copy and paste the current definition of your pauseFcn?
Also, can you try to replace 'bdroot', with the name of your Simulink model? It may not what you want in the end, but It will help in debugging the issue.
Also, can you check if anything appears in the command window related to timer error when you run the Simulink model?
Ameer Hamza
on 27 Apr 2020
I am glad that this method was helpful.
Sunil Subedi
on 11 Sep 2020
Dear Ameer Hamza and Golden,
I have been working on similar purpose simulation and encountered similar problem.
First, I want to explain what I want to do.
Lets say I have two models : Model A and Model B. I want to run the models and pause the simulation and send some data tbetween the Models and run the Models and so on.
During Pause I want to run the script file telling that exchange the data.
After exchanging continue the simulation.
I was using same function inside assertion block.(matlab 2019b)
set_param('Test_Simulink_Model','SimulationCommand','pause');
run('myscript.m');
set_param('Test_Simulink_Model','SimulationCommand','continue')
But what I found was it is oly updating the constant blocks at the starting of the simulation but not during the simulation or each time after the simulation is pause.
I am also curious how to pause the simulation in each time step and update the simulation and continue.
Thank You
More Answers (0)
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)