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

9 次查看(过去 30 天)
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