Filter löschen
Filter löschen

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

9 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Jeff
Jeff am 15 Jan. 2024
Bearbeitet: Jeff am 15 Jan. 2024
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 Kommentar
Fangjun Jiang
Fangjun Jiang am 15 Jan. 2024
Bearbeitet: Fangjun Jiang am 15 Jan. 2024
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 13 Dez. 2023
"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".

Kategorien

Mehr zu Simulink Functions finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by