change the simulation stop time from a block
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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.
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.
The attached model is exported in R2017a.
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');
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?
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.
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
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.
great. it works. thanks a lot.
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
