Main Content

updatePortsFromChoices

Update ports in Variant Component block to match its variant choices

Since R2024a

    Description

    example

    updatePortsFromChoices(variantComponent,Mode=portOption) aligns the ports of the Variant Component block variantComponent with the number and types of its variant choices using a method specified by portOption.

    Examples

    collapse all

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

    Open this model that contains a Variant Component block VariantComp. The VariantComp block has two variant choices Component and Component1. Each variant choice has two input ports inport1 and inport2, and one output port outport1. The variantComp block has no ports.

    model = systemcomposer.createModel("archModel");
    arch = get(model,"Architecture");
    variantComp = addVariantComponent(arch, "VariantComp");
    choiceComp1 = model.lookup(Path='archModel/VariantComp/Component');
    addPort(choiceComp1.Architecture, "inport1", "in");
    addPort(choiceComp1.Architecture, "inport2", "in");
    addPort(choiceComp1.Architecture, "outport1", "out");
    choiceComp2 = model.lookup(Path='archModel/VariantComp/Component1');
    addPort(choiceComp2.Architecture, "inport1", "in");
    addPort(choiceComp2.Architecture, "inport2", "in");
    addPort(choiceComp2.Architecture, "outport1", "out");
    

    The simulation fails because there are no inputs to inport1, inport2, and outport1 due to the absence of the corresponding ports in the VariantComp block.

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

    updatePortsFromChoices(variantComp,Mode="addPorts")
    systemcomposer.openModel("archModel");

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

    Open the model that contains the Variant Component block VariantComp. The VariantComp block has two variant choices Component and Component1. Each variant choice has an input port inport1 and an output port outport1. However, the VariantComp block includes an unused port input2, which is not used by any of its variant choices. Furthermore, the VariantComp block is missing the two required ports, inport1 and outport1 for its variant choices.

    model = systemcomposer.createModel("archModel");
    variantComp = addVariantComponent(model.Architecture, "VariantComp");
    archOfVariantComp = variantComp.OwnedArchitecture;
    archOfVariantComp.addPort("inport2", "in");
    choiceComp1 = model.lookup(Path="archModel/VariantComp/Component");
    addPort(choiceComp1.Architecture, "inport1", "in");
    addPort(choiceComp1.Architecture, "outport1", "out");
    choiceComp2 = model.lookup(Path="archModel/VariantComp/Component1");
    addPort(choiceComp2.Architecture, "inport1", "in");
    addPort(choiceComp2.Architecture, "outport1", "out");
    

    The simulation fails because there is no input to inport1 and outport1 due to the absence of the corresponding ports in the VariantComp block. Additionally, Simscape™ displays a warning message due to the unused input port inport2.

    To match the number and types of the ports of the VariantComp block with its variant choices, use this command. This command adds the missing input port inport1 and output port outport1, and deletes the unused input port inport2 from the VariantComp block. The model now simulates successfully.

    updatePortsFromChoices(VariantComp,Mode="addAndDeletePorts")
    systemcomposer.openModel("archModel");

    Input Arguments

    collapse all

    Variant component, specified as a systemcomposer.arch.VariantComponent object.

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

    • "addPorts" — Adds missing ports to the Variant Component 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 Component 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 Component 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 Component block, using the updatePortsFromChoices 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.

    More About

    collapse all

    Definitions

    TermDefinitionApplicationMore Information
    variant

    A variant is one of many structural or behavioral choices in a variant component.

    Use variants to quickly swap different architectural designs for a component while performing analysis.

    Create Variants
    variant control

    A variant control is a string that controls the active variant choice.

    Set the variant control programmatically to control which variant is active.

    Set Variant Control Condition

    Tips

    Use the updatePortsFromChoices function for Variant Component 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 Component block, the Variant Assembly Component 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 Component or Variant Assembly Component block by using the Fix It options in the Diagnostic Viewer window. When simulating a model with missing or unused ports in a Variant Component or Variant Assembly Component block, the error messages provide Fix It options to add missing ports and remove unused ports from the Variant Component or Variant Assembly Component block for proper alignment with the underlying variant choices.

    Version History

    Introduced in R2024a