現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
pause simulation and do calculation
5 ビュー (過去 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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)
