Main Content

Create Structures in MATLAB Function Blocks

Here is the workflow for creating a structure in a MATLAB Function block:

  1. Decide on the type (or scope) of the structure (see Types of Structures in MATLAB Function Blocks).

  2. Based on the scope, follow these guidelines for creating the structure:

    For Structure Scope:Follow These Steps:
    Input

    1. Create a Simulink.Bus object in the base workspace to define the structure input.

    2. Add data to the MATLAB Function block, as described in Adding Data to a MATLAB Function Block. The data should have the following properties

      • Scope = Input

      • Type = Bus: <object name>

        For <object name>, enter the name of the Simulink.Bus object that defines the structure input

    See Rules for Defining Structures in MATLAB Function Blocks.

    Output

    1. Create a Simulink.Bus object in the base workspace to define the structure output.

    2. Add data to the MATLAB Function block with the following properties:

      • Scope = Output

      • Type = Bus: <object name>

        For <object name>, enter the name of the Simulink.Bus object that defines the structure output

    3. Define and initialize the output structure implicitly as a variable in the MATLAB® function, as described in Structure Definition for Code Generation.

    4. Make sure the number, type, and size of fields in the output structure variable definition match the properties of the Simulink.Bus object.

    Local

    Define the structure implicitly as a local variable in the MATLAB function, as described in Structure Definition for Code Generation. By default, local variables in MATLAB Function blocks are temporary.

    Persistent

    Define the structure implicitly as a persistent variable in the MATLAB function.

    Parameter

    1. Create a structure variable in the base workspace.

    2. Add data to the MATLAB Function block with the following properties:

      • Name = same name as the structure variable you created in step 1.

      • Scope = Parameter

    See Define and Use Structure Parameters.

Use Nonvirtual Buses with MATLAB Function Blocks

In this example model, the MATLAB Function block includes MATLAB code that creates a structure. If a MATLAB Function block outputs a structure, then you must use a Simulink.Bus object to define the bus output.

To see the structure definition, double-click the MATLAB Function block.

Open the Bus Editor and expand the Bus object definition that represents the structure.

To see how the Bus object defines the bus output for the MATLAB Function block, in the MATLAB Toolstrip, on the Editor tab, click Edit Data and then click y. The output Type is defined as the function_bus Bus object.

Related Topics