Main Content

Simulink.VariantUtils.updateVariantSubsystemPorts

Update ports in Variant Subsystem block to match its variant choices

Since R2024a

    Description

    Simulink.VariantUtils.updateVariantSubsystemPorts(blockPathOrHandle,Mode=portOption) aligns the ports of the Variant Subsystem block blockPathOrHandle with the number and types of its variant choices using a method specified by portOption.

    example

    Examples

    collapse all

    This example explains how to add missing ports to a Variant Subsystem block to align them with the number and type of ports in its variant choices.

    Open the slexVariantSubsystems model. The Variant Subsystem block Controller has three input ports, sensor1, sensor2, and sensor3, and one output port, Out1, matching the ports of its variant choices.

    model = "slexVariantSubsystems";
    open_system(model)
    

    Add an input port sensor4 to the variant choice Nonlinear Controller and simulate the model. The simulation fails because there is no input to sensor4 due to the absence of the corresponding input port in the Controller block.

    blockPath = "slexVariantSubsystems/Controller/";
    nonlinearCtrlBlk = blockPath+"Nonlinear Controller/";
    addBlock = nonlinearCtrlBlk+"Add/";
    sensor4 = nonlinearCtrlBlk+"sensor4/";
    add_block("simulink/Sources/In1",sensor4);
    set_param(addBlock,"Inputs","++++");
    add_line(nonlinearCtrlBlk,"sensor4/1","Add/4","autorouting","smart");
    Simulink.BlockDiagram.arrangeSystem(nonlinearCtrlBlk)
    open_system(nonlinearCtrlBlk)
    

    To add the missing input port sensor4 to the Controller block and match the number of input ports with the variant choices, use this command. The model now simulates successfully.

    Simulink.VariantUtils.updateVariantSubsystemPorts...
    (blockPath,Mode="addPorts")
    open_system(blockPath)
    

    This example explains how to add missing ports and delete unused ports from a Variant Subsystem block to align them with the number and types of ports in its variant choices.

    Open the slexVariantSubsystems model. The Variant Subsystem block Controller has three ports, sensor1, sensor2, and sensor3, and one output port, Out1, matching the ports of its variant choices.

    open_system("slexVariantSubsystems");
    

    Add an input port sensor4 to the variant choice Nonlinear Controller and an input port sensor5 to the Controller block and simulate the model. The simulation fails because there is no input to sensor4 due to the absence of the corresponding input port in the Controller block. Additionally, Simulink displays a warning message due to the unused input port sensor5.

    blockPath = "slexVariantSubsystems/Controller/";
    nonlinearCtrlBlk = blockPath+"Nonlinear Controller/";
    addBlock = nonlinearCtrlBlk+"Add/";
    sensor4 = nonlinearCtrlBlk+"sensor4/";
    add_block("simulink/Sources/In1", sensor4);
    set_param(addBlock,"Inputs","++++");
    add_line(nonlinearCtrlBlk,"sensor4/1","Add/4","autorouting","smart");
    Simulink.BlockDiagram.arrangeSystem(nonlinearCtrlBlk)
    open_system(nonlinearCtrlBlk)
    

    sensor5 = blockPath+"sensor5";
    add_block("simulink/Sources/In1",sensor5);
    set_param(sensor5,"Position",[70 208 100 222])
    open_system(blockPath)
    

    To match the number and types of the ports of the Controller block with its variant choices, use this command. This command adds the missing input port sensor4 block and deletes the unused input port sensor5 from the Controller block. The model now simulates successfully.

    Simulink.VariantUtils.updateVariantSubsystemPorts...
    (blockPath,Mode="addAndDeletePorts")
    open_system(blockPath)
    

    Input Arguments

    collapse all

    Path or handle of the Variant Subsystem block to update the ports for, specified as a character vector or a string scalar (for a block path) or double scalar (for a handle).

    Example: 'slexVariantSubsystem/Controller'

    Data Types: char | string | double

    Option to update ports in Variant Subsystem block to match its variant choices, specified as "addPorts" or "addAndDeletePorts".

    • "addPorts" — Adds missing ports to the Variant Subsystem block to align them with the number and types of ports in its variant choices.

    • "addAndDeletePorts" — Adds missing ports and deletes unused ports in the Variant Subsystem block to align them with the number and types of ports in the variant choices. The deleted ports cannot be recovered, and any associated properties are lost.

    Limitations

    • In a Variant Subsystem block, each control port must be unique and have a distinct type when the Variant activation time parameter is set to an activation time other than update diagram. If the variant choices have two different control ports of the same type, and those ports are missing in the Variant Subsystem block, using the Simulink.VariantUtils.updateVariantSubsystemPorts function attempts to add both the missing control ports. However, this results in a simulation error. Therefore, if you need to add multiple control ports of the same type in the Variant Subsystem block, you must change the Variant activation time parameter to update diagram.

    • The Simulink.VariantUtils.updateVariantSubsystemPorts function cannot be used on a Variant Subsystem block when it is within a locked library, is a linked block, or is nested within a linked block whose top-level block is not configured for self-modification. This restriction ensures that updates to variant subsystem ports adhere to the structural integrity of locked or non-modifiable libraries. For more information, see Self-Modifiable Linked Subsystems.

    Tips

    Use the Simulink.VariantUtils.updateVariantSubsystemPorts function for Variant Subsystem blocks with the Allow flexible interface parameter set to on. This setting allows variations in the number of ports between the block and its variant choices, ensuring relevant automated port synchronization. Unlike the Variant Subsystem block, the Variant Assembly Subsystem block does not enforce a strict set of ports. Therefore, you can use this function without any additional configuration.

    Alternative Functionality

    You can align ports in a Variant Subsystem or Variant Assembly Subsystem block by using the Fix It options in the Diagnostic Viewer window. When simulating a model with missing or unused ports in a Variant Subsystem or Variant Assembly Subsystem block, the error messages provide Fix It options to add missing ports and remove unused ports from the Variant Subsystem or Variant Assembly Subsystem block for proper alignment with the underlying variant choices.

    Version History

    Introduced in R2024a