이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)
