Main Content

Implement MATLAB Functions in Simulink with MATLAB Function Blocks

MATLAB Function blocks enable you to define custom functions in Simulink® models by using the MATLAB® language. MATLAB Function blocks support C/C++ code generation from Simulink Coder™ and Embedded Coder®.

Use these blocks when:

  • You have an existing MATLAB function that models custom functionality, or it is easy for you to create such a function.

  • Your model requires custom functionality that is not or cannot be captured in the Simulink graphical language.

  • You find it easier to model custom functionality by using a MATLAB function than by using a Simulink block diagram.

  • The custom functionality that you want to model does not include continuous or discrete dynamic states. To model dynamic states, use S-functions. See Create and Configure MATLAB S-Functions.

Calculate Mean and Standard Deviation with a MATLAB Function Block

This example starts with a model that includes a MATLAB Function block and guides you through how to customize the block to calculate the mean and standard deviation for a vector of values.

Open the Model

The model call_stats_block1 includes an empty MATLAB Function block and the blocks you need for the example. Open the model.

Program the MATLAB Function Block

Program the block to calculate the mean and standard deviation for a vector of values.

  1. To open the MATLAB Function Block Editor, double-click the MATLAB Function block. A default function appears in the MATLAB Function Block Editor with two variables: one input and one output argument.

    this image shows the MATLAB Function Block Editor after it has been opened from the call_stats_block1 model. It includes a default function.

  2. Define the function inputs and outputs by editing the function declaration statement:

    function [mean, stdev] = stats(vals)

    This statement defines a function called stats with three variables. The statement defines an input argument, vals, and two output arguments, mean and stdev.

  3. On a new line after the function declaration statement, add the following code:

    % Calculates a statistical mean and a standard
    % deviation for the values in vals.
    len = length(vals);
    mean = avg(vals,len);
    stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
    function mean = avg(array,size)
    mean = sum(array)/size;
  4. Exit the block. The block updates the port names. The function outputs mean and stdev correspond to block output ports mean and stdev and the function input vals corresponds to the block input port vals.

  5. Complete the connections to the MATLAB Function block as shown.

    This shows connected signals between the blocks established in the previous image.

  6. Save the model as call_stats_block2.

Check Properties for MATLAB Function Block Variables

You can check and manage properties for variables in MATLAB Function blocks. See Create and Define MATLAB Function Block Variables. In this example, verify that the input argument vals inherits its type and size properties:

  1. Double-click the MATLAB Function block.

  2. Open the Symbols pane and the Property Inspector. In the Function tab, click Edit Data.

    The Symbols pane displays the variable names in the Name column. The Type column determines the function argument port assignment and where it appears in the function declaration statement. In this example, vals is assigned to an input port. mean and stdev are assigned to output ports. If you change the Type entry for either variable, the function declaration statement also changes.

  3. Check the properties for vals. In the Symbols pane, click the row for vals. The Property Inspector updates to show the properties for vals. The Scope property matches the Type column.

    Only some properties can be inherited: Size, Unit, Complexity, and Type. Variables indicate that they inherit these properties when Size is -1, Unit is inherit, Complexity is Inherited, and Type is Inherit: Same as Simulink. In this example, vals uses these values for Size and Type.

Check the other MATLAB Function block variables by selecting them in the Symbols pane.

By default, newly defined input and output variables inherit their size, unit, complexity, and type. If you want the variables to use a specific size, unit, or type, you can specify these properties explicitly. For example, enter [2 3] in the Size property to specify the variable as a 2-by-3 matrix. For more information, see Define and Modify Variable Data Types and Specify Size of MATLAB Function Block Variables.

Build the Function

After you enter code in a MATLAB Function block, you can check the code for errors and build the function. See Debug MATLAB Function Blocks.

When you build a model, MATLAB uses a compiler to compile your MATLAB function block code. If you have multiple MATLAB-supported compilers installed on your system, you can change the default compiler using the mex -setup command. See Change Default Compiler. To see a list of supported compilers, open Supported and Compatible Compilers, click the tab that corresponds to your operating system, find the Simulink Product Family table, and go to the For Model Referencing, Accelerator mode, Rapid Accelerator mode, and MATLAB Function blocks column.

In Simulink, click Run to simulate the model. The software builds the MATLAB Function block when it builds the model and outputs the data from the function to the two Display blocks.

Prepare the Block for Code Generation

To generate standalone C/C++ code from a model that contains MATLAB Function blocks, use Simulink Coder or Embedded Coder.

Select a Supported Compiler for Simulation and Code Generation Builds

To see a list of supported compilers, open Supported and Compatible Compilers, click the tab that corresponds to your operating system, find the Simulink Product Family table, and go to the Simulink Coder column.

Generate Code for the MATLAB Function Block

If you have a license for Simulink Coder or Embedded Coder, you can generate code from the model. To generate code:

  1. In the call_stats_block2 model, double-click the MATLAB Function block.

  2. Open the Simulink Coder or Embedded Coder app.

  3. On the C Code tab, Build to compile and build the example model.

    If you get an error related to the Variable-step solver, open the Configuration Parameters window, then click Solver in the left pane. Under Solver selection, set Type to Fixed-step and generate code again. To learn more about the differences between fixed-step and variable-step solvers, see Fixed-Step Versus Variable-Step Solvers.

See Also

| |

Related Topics