pause simulation and do calculation
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Ameer Hamza
2020-4-23
See breakpoints in MATLAB: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html
21 个评论
Golden
2020-4-23
not about pausing m file, i'm talking pausing the simulation of a model file. thanks.
Ameer Hamza
2020-4-23
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
2020-4-23
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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.
Golden
2020-4-24
this time it does not complain anymore but it still stay at the pause - i had to click the continue button. this is a tough cookie. sorry for the hassle.
Ameer Hamza
2020-4-25
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?
Golden
2020-4-27
Only now I realized that you warned me about using two single quotation marks, not double quotation marks, on SimulationCommand and Continue.
Now using the single quotation marks it is working. Thanks a lot.
Sunil Subedi
2020-9-11
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Schedule Model Components 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
