change the simulation stop time from a block
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I understand you may change the simulation stop time using set-param function. However, I'd like to do the same thing with a block. This way I can make it to a library so that people just take it from library and drop to his/her model. I need to be able to change the stop time which is calcuated by this block. Thanks.
採用された回答
Ameer Hamza
2020 年 4 月 7 日
You can stop the simulation at any using stop block: https://www.mathworks.com/help/simulink/slref/stopsimulation.html. You can define a condition, and when the condition becomes true, the simulation stops. Note, this method will only allow decreasing the simulation time. If you want to run the simulation, longer than the stop-time, I am not sure whether it is possible.
16 件のコメント
madhan ravi
2020 年 4 月 7 日
Golden comments:
Thanks for the reply. As you rightly pointed out, Stop block can only allow decreasing the simulation time. I tried it and simulation stops at the lesser of the two: model stop time and the stop block condition time. Thanks for trying though.
I just tried and found that it is possible to change the simulation time (both increase and decrease) from inside the simulink_model using the MATLAB function block. See the attached model. You can try by pasting the following code in a MATLAB function block.
function fcn()
coder.extrinsic('set_param');
coder.extrinsic('gcs');
set_param(gcs, 'StopTime', '5');
This will change the simulation time to 5, despite whatever is set by the user.
Golden
2020 年 4 月 8 日
Thank you for the reply. I'd like to try it, but I have 2017a version for now. Can you attach a 2017a version of the model. Thanks.
Ameer Hamza
2020 年 4 月 8 日
The attached model is exported in R2017a.
Golden
2020 年 4 月 8 日
I got an error running your model:
An error occurred while running the simulation and the simulation was terminated
Caused by:
SubSystem block does not have a parameter named 'StopTime'
Am I missing something?
Golden, In the MATLAB function block, try to replace 'gcs' in the last line with the name of your simulink model
set_param('model_name', 'StopTime', '5')
Yes. With the model_name it works.
Is there any way to not need to manually change the model_name every time it is used in a different model? I intend this to be a drop and play block. I know I am asking a lot, but you are the expert. Thanks.
Also, you saw you used coder.extrinsic functions. Does it mean user has to have Simulink coder to be able to run this?
No, simulink coder is not needed to use coder.extrinsic.
In my original code, I used gcs to pick up the model name automatically, but it turns out that it will fail in some cases. Replace the code inside the MATLAB function block to
coder.extrinsic('set_param');
coder.extrinsic('bdroot');
set_param(bdroot, 'StopTime', '5');
Ameer Hamza
2020 年 4 月 8 日
Golden's comment moved here:
here is what I got:
Function 'bdroot' is not supported for code generation. Consider adding coder.extrinsic('bdroot') at the top of the function to bypass code generation.
Function 'MATLAB Function' (#46.87.93), line 5, column 11:
"bdroot"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Golden, have you added the line
coder.extrinsic('bdroot');
as I gave in my comment?
Golden
2020 年 4 月 8 日
it turned out I somehow had extra white spaces in the strings. Now it is good.
A further question: instead of a constant of 5 seconds for example, I have to calculate the stop time in the initialize callback function with the user's inputs in the mask. How do you convert the stop time to a string? The Matlab function num2str definitely is not ok anymore.
Thanks alot.
Since your block will just be limited to simulations and it will not be used for code generation, you can add the line
coder.extrinsic('num2str');
and then use num2str in your Simulink code.
Golden
2020 年 4 月 8 日
Well, i still can't get it to work. Here is the problem: I have Tend initialized in the mask of this block. When running this MATLAB function I got this:
Undefined function or variable 'Tend'.
Function 'VenableBlock2/MATLAB Function' (#58.141.145), line 6, column 39:
"Tend"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Ameer Hamza
2020 年 4 月 8 日
See the attached file. First, create a parameter in mask and then follow this: https://www.mathworks.com/help/simulink/ug/parameter-arguments-in-matlab-function-block-functions.html to make the parameter visible inside the MATLAB function block.
Golden
2020 年 4 月 8 日
great. it works. thanks a lot.
Ameer Hamza
2020 年 4 月 9 日
I am Glad to be of help.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Programmatic Model Editing についてさらに検索
製品
参考
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)
