Documentation

Combine Buses into an Array of Buses

Tip

金宝app®provides several techniques for combining signals into a composite signal. For a comparison of techniques, seeTypes of Composite Signals.

What Is an Array of Buses?

An array of nonvirtual buses is an array whose elements are buses. Each bus object has the same signal name, hierarchy, and attributes for its bus elements.

An example of using an array of buses is to model a multi-channel system, such as a communications system. You can model all the channels using the same bus object, although each of the channels could have a different value.

To use an array of buses:

For an example of a model that uses an array of buses, open thesldemo_bus_arraysmodel. In this example, the nonvirtual bus input signals connect to aVector ConcatenateorMatrix Concatenateblock that creates an array of bus signals. Here is the diagram after you update it:

The model uses the array of buses with:

  • AnAssignmentblock, to assign a bus in the array

  • AFor Each Subsystemblock, to perform iterative processing over each bus in the array

  • AMemoryblock, to output the array of buses input from the previous time step

Benefits of an Array of Buses

Use an array of buses to:

  • Represent structured data compactly.

    • Reduce model complexity.

    • Reduce maintenance by centralizing algorithms used for processing multiple buses.

  • Streamline iterative processing of multiple buses of the same type, for example, by using a For Each Subsystem with the array of buses.

  • Simplify changing the number of buses, without your having to restructure the rest of the model or make updates in multiple places in the model.

  • Use built-in blocks, such as theAssignmentorSelectorblocks, to manipulate arrays of buses just like arrays of any other type. Using a array of buses avoids the need for you to create custom S-functions to manage packing and unpacking structure signals.

  • Use the combined bus data across subsystem boundaries, model reference boundaries, and into or out of aMATLAB Functionblock.

  • Keep all the logic in the Simulink model, rather than splitting the logic between C code and the Simulink model. This approach supports integrated consistency and correctness checking, maintaining metadata in the model, and avoids the need to manage model components in two different environments.

  • Generate code that has an array of C structures, which you can integrate with legacy C code that uses arrays of structures. This approach simplifies indexing into an array for Simulink computations, using aforloop on indexed structures.

Define an Array of Buses

For information about the kinds of buses that you can combine into an array of buses, seeBus Requirements.

To define an array of buses, use aConcatenateblock. The table describes the array of buses input requirements and output for each of theVector Concatenateand theMatrix Concatenateversions of theConcatenateblock.

Block Bus Signal Input Requirement Output
Vector Concatenate

Vectors, row vectors, or columns vectors

If any of the inputs are row or column vectors, output is a row or column vector.

Matrix Concatenate

Signals of any dimensionality (scalars, vectors, and matrices)

Trailing dimensions are assumed to be 1 for lower dimensionality inputs.

Concatenation is on the dimension that you specify with theConcatenate dimensionparameter.

Note

Do not use aMuxblock or aBus Creatorblock to define an array of buses. Instead, use aBus Creatorblock to create scalar bus signals.

  1. Defineonebus object forallof the buses that you want to combine into an array of buses. For information about defining bus objects, seeSpecify Bus Properties with Bus Objects.

    Thesldemo_bus_arraysmodel defines ansldemo_bus_arrays_busobjectbus object, which both of the Bus Creator blocks use for the input bus signals (Scalar Bus) for the array of buses.

  2. Add aVector ConcatenateorMatrix Concatenateblock to the model and open the Block Parameters dialog box.

    Thesldemo_bus_arrays_busobjectmodel uses a Vector Concatenate block, because the inputs are scalars.

  3. Set theNumber of inputs参数是公交车的数量that you want to be in the array of buses.

    The block icon displays the number of input ports that you specify.

  4. Set theModeparameter to match the type of the input bus data.

    In thesldemo_bus_arraysmodel, the input bus data is scalar, so theModesetting isVector.

  5. If you use aMatrix Concatenationblock, set theConcatenate dimensionparameter to specify the output dimension along which to concatenate the input arrays. Enter one of the following values:

    • 1— concatenate input arrays vertically

    • 2— concatenate input arrays horizontally

    • A higher dimension than 2 — perform multidimensional concatenation on the inputs

  6. Connect to theConcatenateblock all the buses that you want to be in the array of buses.

Group Constant Signals into an Array of Buses

您可以使用一个Constantblock to compactly represent multiple constant-valued signals as an array of buses. You can use this technique to reduce the number of signal lines in a model and the number of variables that the model uses, especially when the model repeats an algorithm with different parameter values.

To generate a constant-valued array of bus signals, use an array of MATLAB®structures in aConstantblock. The block output is an array of buses, and each field in the array of structures provides the simulation value for the corresponding signal element.

You can also use an array of structures to specify theValue财产的Simulink.Parameterobject, and use the parameter object in aConstantblock.

  1. Open the example modelex_constantbus_array.

    The variablesParamBusandconst_param_structappear in the base workspace. The variableconst_param_structcontains a structure whose field names match the elements of the bus type thatParamBusdefines.

  2. Update the diagram to view the signal line widths.

    The output of theConstantblock is a single scalar bus of typeParamBus. The structure variableconst_param_structspecifies the constant value in the block.

  3. At the command prompt, create an array of two structures by copying the structureconst_param_struct. Customize the field values by indexing into the individual structures in the array.

    const_struct_array =...[const_param_struct const_param_struct]; const_struct_array(2).Offset = 158; const_struct_array(2).Gain = 3.83; const_struct_array(2).Threshold = 1039.77
    const_struct_array = 1x2 struct array with fields: Offset Gain Threshold

  4. In theConstantblock dialog box, setConstant valuetoconst_struct_array.

  5. Add twoSelectorblocks to the model, and connect theConstantblock as shown.

  6. In the Selector block dialog box, setIndexto1andInput port sizeto2.

  7. In the Selector1 block dialog box, setIndexto2andInput port sizeto2.

  8. Copy the block algorithm in the model, and connect the blocks as shown.

  9. Update the diagram. The signal line width and style indicate that the output of theConstantblock is an array of buses. TheSelector块每提取一个公交车in the array.

    Each copy of the algorithm uses the constant values provided by the corresponding structure in the variableconst_struct_array.

To create an array of structures for a bus that uses a large hierarchy of signal elements, consider using the functionSimulink.Bus.createMATLABStruct. You can also use this technique to create an array of structures if you do not have a scalar structure that you can copy.

See Also

Blocks

Related Topics