필터 지우기
필터 지우기

How to add blocks and lines to Simulink subsystem upon model initialization?

조회 수: 9 (최근 30일)
Jeff
Jeff 2023년 12월 13일
편집: Jeff 2024년 1월 15일
I am developing a feature for an existing software tool which uses a MATLAB script to set the inputs for a Simulink model. The feature includes a block which takes inputs using From blocks (based on a number specified in the input script) and outputs to multiple Goto blocks (based on the same number). For example, if a user inputs a 2 for this variable, there should be 2 From blocks and 2 Goto blocks added and respective lines.
I would like to add these blocks programmatically upon model initialization. First, I tried using a Mask Initialization function, but this led to the function being evaluated multiple times and duplicate blocks were created, leading to error.
I thought about variant subsystems, but it does not seem that they can be defined for an unspecified number of blocks.
Next, I tried using the InitFcn Callback. This seems to work, but after reading more (https://www.mathworks.com/help/simulink/ug/initialization-function.html), I found that this approach is specifically advised against. Why is this not recommended? And are there any other approaches to modifying a subsystem struture on initialization?

채택된 답변

Jeff
Jeff 2024년 1월 15일
편집: Jeff 2024년 1월 15일
I was able to figure out my own issue.
The suggested approach does not work because I need the blocks to be created each time the simulation is run based on the inputs. For example, if the inputs specify 2 blocks, I need 2 blocks created. This means, it doesn't just need to occur once, it should occur every time the simulation is run.
I found the most intuitive method (and consistent with other Library blocks in the software tool) is to create a mask which reads in variables (inputs) from the Workspace, compares the inputs to the existing blocks, deletes previous blocks if they exist, and adds the new blocks and lines according to the inputs. This ensures that the blocks in the Simulink model are always consistent with the inputs.
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2024년 1월 15일
편집: Fangjun Jiang 2024년 1월 15일
I "un-accepted" the answer because there is not enough info regarding answering the question and providing help for others. Please provide more details or close the question.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2023년 12월 13일
"I would like to add these blocks programmatically upon model initialization". What is your definition of "model initialization"?
Try to see if you can fit your need into one of these callbacks. I don't think there is anything specifically against using InitFcn as long as it is used as it intended. In your case, the blocks only need to be added once. They don't need to be added every time the simulation is run, so InitFcn is not the rigth callback to use.
It sounds like the callback should be triggered by some action on your "existing software tool".

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by