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
Ameer Hamza 2020 年 4 月 7 日

0 投票

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
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.
Ameer Hamza
Ameer Hamza 2020 年 4 月 7 日
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
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
Ameer Hamza 2020 年 4 月 8 日
The attached model is exported in R2017a.
Golden
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?
Ameer Hamza
Ameer Hamza 2020 年 4 月 8 日
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')
Golden
Golden 2020 年 4 月 8 日
編集済み: Golden 2020 年 4 月 8 日
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?
Ameer Hamza
Ameer Hamza 2020 年 4 月 8 日
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
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
Ameer Hamza
Ameer Hamza 2020 年 4 月 8 日
Golden, have you added the line
coder.extrinsic('bdroot');
as I gave in my comment?
Golden
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.
Ameer Hamza
Ameer Hamza 2020 年 4 月 8 日
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
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
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
Golden 2020 年 4 月 8 日
great. it works. thanks a lot.
Ameer Hamza
Ameer Hamza 2020 年 4 月 9 日
I am Glad to be of help.

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

その他の回答 (0 件)

カテゴリ

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

製品

質問済み:

2020 年 4 月 7 日

コメント済み:

2020 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by