Connect Structures in MATLAB Functions to Simulink Bus Signals

About Structures in MATLAB Functions

MATLAB® functions support MATLAB structures. You can create structures in top-level MATLAB functions to interface with Simulink® bus signals at input and output ports. Simulink buses appear inside the MATLAB function as structures; structure outputs from the MATLAB function appear as buses.

You can also create structures as local and persistent variables in top-level functions and local functions of MATLAB functions.

Define Structures in MATLAB Functions

This section describes how to define structures in MATLAB functions.

Rules for Defining Structures in MATLAB Functions

Follow these rules when defining structures for MATLAB functions in Stateflow® charts:

  • For each structure input or output in a MATLAB function, you must define a Simulink.Bus object in the base workspace to specify its type to the Simulink signal.

  • MATLAB structures cannot inherit their type from Simulink signals.

  • MATLAB functions support nonvirtual buses only (see Types of Composite Signals (Simulink)).

  • Structures cannot have scopes defined as Constant.

Define Structure Inputs and Outputs to Interface with Bus Signals

When you create structure inputs in MATLAB functions, the function determines the type, size, and complexity of the structure from the Simulink input signal. When you create structure outputs, you must define their type, size, and complexity in the MATLAB function.

You can connect MATLAB structure inputs and outputs to any Simulink bus signal, including:

  • Simulink blocks that output bus signals — such as Bus Creator blocks

  • Simulink blocks that accept bus signals as input — such as Bus Selector and Gain blocks

  • S-Function blocks

  • Other MATLAB functions

To define structure inputs and outputs for MATLAB functions in Stateflow charts, follow these steps:

  1. Create a Simulink bus object in the base workspace to specify the properties of the structure you will create in the MATLAB function.

    For information about how to create Simulink bus objects, see Create and Specify Simulink.Bus Objects (Simulink).

  2. Open the Model Explorer and follow these steps:

    1. In the Model Hierarchy pane, select the MATLAB function in your chart.

    2. Add a data object, as described in Add Data Through the Model Explorer.

      The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.

    3. In the Properties dialog box, enter the following information in the General tab fields:

      FieldWhat to Specify
      NameEnter a name for referencing the structure in the MATLAB function. This name does not have to match the name of the bus object in the base workspace.
      ScopeSelect Input or Output.
      Type

      Select Bus: <bus object name> from the drop-down list.

      Then, replace “<bus object name>” with the name of the Simulink.Bus object in the base workspace that defines the structure. For example: Bus: inbus.

    4. To add or modify Simulink.Bus objects, open the Data Type Assistant. Then, click the Edit button to open the Simulink Bus Editor (see Create and Specify Simulink.Bus Objects (Simulink)).

    5. Click Apply.

  3. If your structure is an output (has scope of Output), define the output implicitly in the MATLAB function to have the same type, size, and complexity as its Simulink.Bus object. For details, see Structures (MATLAB Coder).

Define Local and Persistent Structure Variables

You can define structures as local or persistent variables inside MATLAB functions. For details, see Structures (MATLAB Coder).