Group Blocks into Subsystems Programmatically - MATLAB & Simulink (original) (raw)
As a model increases in size and complexity, you can simplify it by grouping blocks into subsystems. A Subsystem block is a block that contains a subset of the contents of a model. For a brief introduction to subsystems, see Group Blocks into Subsystems. For more information about subsystems, see Subsystems.
The table shows examples of how to programmatically create and work with subsystems. For more syntaxes and examples, see the function documentation. For information about how to get model and block handles, see Get Handles and Paths. To configure subsystem port labels, see Configure Subsystem Port Labels.
The model, library, or subsystem in which you want to create or edit a subsystem must be loaded. For information about how to load models, libraries, and subsystems, see Create, Load, Open, Save, and Close Models Programmatically.
Action | Function | Example |
---|---|---|
Create a new subsystem. | add_block | In a model named myModel, create an empty subsystem called mySubsystem. Then, add a Gain block namedmyBlock to the subsystem.Create the subsystem by adding a new Subsystem block to the model.add_block("simulink/Ports & Subsystems/Subsystem", ... "myModel/mySubsystem");Add a Gain block in the new subsystem.add_block("simulink/Math Operations/Gain", ... "myModel/mySubsystem/myBlock"); |
Create a new subsystem from an existing set of blocks. | Simulink.BlockDiagram.createSubsystem | Create a new subsystem from all the blocks in the root level of a model with the handle hModel.Get the handles of all blocks in the top level of the model.hBlocks = Simulink.findBlocks(hModel);Create the subsystem.Simulink.BlockDiagram.createSubsystem(hBlocks); |
Copy the contents of a model, library, or component into an empty subsystem you specify. | Simulink.BlockDiagram.copyContentsToSubsystem | Copy the contents of a model with the handle hModel into an empty subsystem in another model. The empty subsystem has the handlehSub.Simulink.BlockDiagram.copyContentsToSubsystem(hModel,hSub) |
Copy the contents of a subsystem into another model. | Simulink.SubSystem.copyContentsToBlockDiagram | Copy the contents of a subsystem represented by a Subsystem block with the handle hSub into a model with the handlehModel.Simulink.SubSystem.copyContentsToBlockDiagram(hSub,hModel) |
Specify whether a subsystem is atomic. An atomic subsystem is treated as a unit when determining the execution order of block methods. For more information, see Specify Whether Subsystem Is Atomic. | set_param | Find out whether the Subsystem block with the handlehSub represents a subsystem that is atomic. Use the get_param function.get_param(hSub,"TreatAsAtomicUnit")Make the subsystem atomic.set_param(hSub,TreatAsAtomicUnit="on")Make the subsystem nonatomic.set_param(hSub,TreatAsAtomicUnit="off") |
Specify subsystem read/write permissions. | set_param | Get the permissions of a subsystem represented by aSubsystem block with the handle hSub. Use theget_param function.get_param(hSub,"Permissions")Set the permissions to only permit viewing, not editing.set_param(hSub,Permissions="ReadOnly")Set the permissions to permit neither viewing nor editing.set_param(hSub,Permissions="NoReadOrWrite")Set the permissions to permit reading and writing.set_param(hSub,Permissions="ReadOrWrite") |
Convert a subsystem into a model reference. The subsystem must be atomic. | Simulink.SubSystem.convertToModelReference | Convert a subsystem represented by a Subsystem block with the handle hSub into a model reference namedmyRef.Simulink.SubSystem.convertToModelReference(hSub,"myRef") |
Replace a subsystem with its contents. Replacing atomic subsystems is not supported. | Simulink.BlockDiagram.expandSubsystem | Replace the subsystem represented by a Subsystem block with the handle hSub1 with an area that contains the subsystem contents.Simulink.BlockDiagram.expandSubsystem(hSub1)Replace the subsystem represented by a Subsystem block with the handlehSub2 with its contents without creating an area.Simulink.BlockDiagram.expandSubsystem(hSub2,CreateArea="Off") |
Delete the contents of a subsystem. | Simulink.SubSystem.deleteContents | Delete the contents of a subsystem represented by aSubsystem block with the handlehSub.Simulink.SubSystem.deleteContents(hSub) |
See Also
load_system | open_system | get_param