Set Simulink model simulation stop time from m file

I want to set my models simulation stop time from an m file, but I'm a little confused about how to go about it. When I'd get the data for blocks I'd use get_param, but I want to get the parameters of the model. Would I go about it the same way? Thanks.

 採用された回答

Kaustubha Govind
Kaustubha Govind 2012 年 6 月 6 日

16 投票

You can set_param for the model also:
set_param('modelname', 'StopTime', '3000')

3 件のコメント

Danijel Domazet
Danijel Domazet 2020 年 5 月 13 日
I tried this and it sets the time OK (after sim is done, I open the diagram and new value is visible), but when I run the simulation sim('test_sim') it does not take this value. Why is this happening?
Could you provide the link to documentation for StopTime?
Ben MacCallum
Ben MacCallum 2021 年 3 月 26 日
If you are using the sim function to run your model, you can set the stop time as follows,
sim('model_name', 'StopTime', '40');
Athira Km
Athira Km 2021 年 8 月 12 日
Thank you but how to set this dynamically from a parameter instead of a constant value?

サインインしてコメントする。

その他の回答 (1 件)

Bill Tubbs
Bill Tubbs 2021 年 11 月 25 日

2 投票

You can also do it by passing it as an argument when you run the simulation from the m file:
sim_out = sim(sim_model, t_stop)

3 件のコメント

Paul
Paul 2022 年 11 月 15 日
This doesn't work anymore. Has the syntax changed recently?
I get the following error:
Expected a string scalar or character vector for the parameter name.
Bill Tubbs
Bill Tubbs 2022 年 11 月 15 日
編集済み: Bill Tubbs 2022 年 11 月 15 日
Hmm. Not sure. Are you converting t_stop to a string? This is the exact statement I am using right now with MATLAB v2021b. Maybe you need to include the option char array like this:
sim_out = sim(sim_model, 'StartTime', '0', 'StopTime', string(t(end)));
Bill Tubbs
Bill Tubbs 2022 年 11 月 15 日
Actually, the original code I posted above where t_stop is a number does also work in 2021b. Maybe this behaviour has been removed in recent versions. Nevertheless, maybe it's best to always include 'StopTime' and use string(t_stop).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeProgrammatic Model Editing についてさらに検索

タグ

質問済み:

2012 年 6 月 6 日

コメント済み:

2022 年 11 月 15 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by