Main Content

Specify Bus Properties with Simulink.Bus Object Data Types

A bus can be associated with aSimulink.Busobject, which specifies properties that Simulink®uses to validate the bus.Busobjects are optional for virtual buses, but required for nonvirtual buses.

ABusobject specifies only the architectural properties of a bus, as distinct from the values of the signals it contains. For example, aBusobject can specify the number of elements in a bus, the order of those elements, whether and how elements are nested, and the data types of constituent signals; but not the signal values.

ABusobject is analogous to a structure definition in C: it defines the members of the bus, but does not create the bus. ABusobject is also similar to a cable connector. The connector defines all the pins and their configuration and controls what types of wires can be connected to it. Similarly, aBusobject defines the configuration and properties of the signals that the associated bus must have.

Simulink.Bus Object Workflow

UsingBusobjects in a model involves performing these tasks, in many cases iteratively.

Determine Whether to Use Simulink.Bus Objects

You must useBusobjects for these modeling configurations:

  • Nonvirtual buses

  • Stateflow®charts with bus input or output

  • S-function or Legacy Code Tool interface with external code

You can associate aBusobject with multiple blocks. Some blocks require that you specify aBusobject if the block has a bus input or output. For more information, seeBus-Capable Blocks.

When aBusobject governs a signal input or output for a block, the signal must be a bus that has the properties specified by the object. Any variance causes an error.

If you useBus CreatororIn Bus Elementblock parameters to specify bus properties, all blocks downstream from the bus inherit the same properties.

You can useBus Creatorblock parameters to define virtual buses and perform limited error checking. To perform thorough error checking on a bus, associate aBusobject with that bus. UsingBusobjects to check buses for errors is important when you want to create reusable and shareable model components.

To make tracing the correspondence between the model and the generated code for a bus easier, use a nonvirtual bus. The generated code for a nonvirtual bus produces a structure. Nonvirtual buses can result in multiple copies of some buses.

Determine How to Manage Simulink.Bus Objects

You can saveBusobjects to these locations:

  • Data dictionary

  • Function

  • MAT-file

  • Database or other external files

If you do not saveBus对象,当你重新打开一个模型,使用theBusobjects, you need to recreate theBus对象。

DifferentBusobject storage locations provide different advantages.

Location Usage Considerations

Data dictionary

Use for large model componentization.

When you save to a data dictionary from the base workspace, you get all the variables used by the model, not just theBus对象。

Before you save to a data dictionary, readConsiderations before Migrating to Data Dictionary.

Function

Use for when you want to use MATLAB®for traceability and model differencing.

MAT-file

Use for fasterBusobject saving and loading.

Database or other external files

Use for comparing bus interface information with design documents stored in an external data source.

Create and Specify Simulink.Bus Objects

To create or editBusobjects interactively, use theBus EditororModel Explorer.Busobjects created with these tools are initially stored in the base workspace or data dictionary. To visualize bus hierarchy and access capabilities such as import and export, use theBus Editor. When you have multiple types of objects to update, use theModel Explorer. TheModel Explorerlets you update additional types of objects, such asSimulink.Parameter对象。

To create and editBusobjects programmatically, seeCreate Bus Objects Programmatically.Busobjects are initially stored in either the base workspace, a data dictionary, or a function.

After you create aBusobject and specify its attributes, you can associate it with any block that needs to use the bus definition that the object provides. To associate a block with a bus, in the Block Parameters dialog box, setOutput data typeorData typetoBus: and replacewith theBusobject name.

You can specify theBusobject as the data type of a block either before or after defining theBusobject. However, before you simulate the model, theBusobject and the corresponding bus must have the same number of bus elements, in the same order. Also, each element in theBusobject and in the corresponding bus must have the same data type and dimensions.

During model development, you can modify buses to matchBusobjects or modifyBusobjects to match buses.

If you do not want to change theBusobject, you can:

  • Create aBusobject that matches the changes to the bus and use the newBusobject for the blocks that the changed bus connects to.

  • Revert the bus changes so that the bus continues to match the associatedBusobject.

Save Simulink.Bus Objects

To saveBusobjects stored in the base workspace, you can use any MATLAB technique that saves the contents of the base workspace. However, the resulting file contains everything in the base workspace, not justBus对象。

You can saveBusobjects in a variety of locations.

Location File Creation Method File Contents

Data dictionary

SeeMigrate Models to Use Simulink Data Dictionary. Busobjects and other base workspace variables used by a model

Function

Use theBus EditororSimulink.Bus.savefunction. Busobjects

MAT file

Use theBus Editor. Busobjects

Database or other external files

Use theSimulink.importExternalCTypesfunction, scripts, or Database Toolbox™ functionality on C code structure (struct) definitions. In preparation for integrating existing algorithmic C code for simulation (for example, by using the Legacy Code Tool), you can package signal or parameter data in the definitions according to structure type.

Busobjects

When you modify savedBusobjects, you must resave them to keep the changes.

Map Simulink.Bus Objects to Models

你模拟一个模型之前,所有的Busobjects it uses must be loaded into the base workspace or a data dictionary used by the model. For automation and consistency across models, mappingBusobjects to models is important.

  • By identifying all of theBusobjects that a model requires, you can ensure that those objects are loaded before model execution.

  • By identifying all models that use aBusobject, you can ensure that changes to aBusobject do not cause unexpected changes in any of the models that use theBusobject.

To ensure the necessaryBusobjects load before model execution, consider:

  • Projects — Automatically load or run files that defineBusobjects by configuring the files to run when you open a project. For details, seeProject Management.

  • Data dictionaries — StoreBusobjects with variables and other objects for one or more models.

    To share aBusobject among models, you can link each model to a dictionary and create a common referenced dictionary to store the object. For an example, seePartition Dictionary Data Using Referenced Dictionaries.

  • Databases — Capture mapping information in an external data source, such as a database.

  • Model callbacks — Load or run files that defineBusobjects by using a model callback, such asPreLoadFcn. For more information, seeModel Callbacks.

    If a model uses only a fewBusobjects, consider copying theBus目标代码直接进入回调,而不是loading a file. For an example, open modelex_bus_tutorial_nonvirtualand examine the callback.

To find where aBusobject is used in an open model, seeFinding Blocks That Use a Specific Variable.

Tip

Using a rigorous and standard naming convention is very helpful for mappingBusobject usage. For example, consider the model and data required for an actuator control function. Naming the modelActuatorand the input and output portsActuator_bus_inandActuator_bus_out, respectively, makes the connection between theBusobjects and the model clear.

Note that this approach can cause issues if the output from one model is fed directly to another model. In this case, the naming mismatch results in an error.

See Also

Tools

Objects

Related Topics