How to add blocks and lines to Simulink subsystem upon model initialization?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Fangjun Jiang
le 13 Déc 2023
0 votes
"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".
Catégories
En savoir plus sur Programmatic Model Editing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!