Documentation Center

  • Trial Software
  • Product Updates

Create Model That Uses MATLAB Function Block

Adding a MATLAB Function Block to a Model

  1. Create a new model with the Simulink® product and add a MATLAB Function block to it from the User-Defined Function library:

  2. Add the following Source and Sink blocks to the model:

    • From the Sources library, add a Constant block to the left of the MATLAB Function block and set its value to the vector [2 3 4 5].

    • From the Sinks library, add two Display blocks to the right of the MATLAB Function block.

    The model should now have the following appearance:

  3. In the Simulink Editor, select File > Save As and save the model as call_stats_block1.

Programming the MATLAB Function Block

The following exercise shows you how to program the block to calculate the mean and standard deviation for a vector of values:

  1. Open the call_stats_block1 model that you saved at the end of Adding a MATLAB Function Block to a Model. Double-click the MATLAB Function block fcn to open it for editing.

    A default function signature appears, along with the %#codegen directive. The directive indicates that you intend to generate code from this algorithm and turns on appropriate error checking (see Compilation Directive %#codegen.

  2. Edit the function header line as follows:

    function [mean,stdev] = stats(vals)
    %#codegen

    The function stats calculates a statistical mean and standard deviation for the values in the vector vals. The function header declares vals as an argument to the stats function, with mean and stdev as return values.

  3. Save the model as call_stats_block2.

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

  5. In the MATLAB Function Block Editor, enter a line space after the function header and 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);
    plot(vals,'-+');
    
    function mean = avg(array,size)
    mean = sum(array)/size;
    

     More about length

     More about len

     More about plot

  6. Save the model as call_stats_block2.

Building the Function and Checking for Errors

After programming a MATLAB Function block in a Simulink model, you can build the function and test for errors. This section describes the steps:

  1. Set up your compiler.

  2. Build the function.

  3. Locate and fix errors.

Setting Up Your Compiler

Building your MATLAB Function block requires a supported compiler. MATLAB automatically selects one as the default compiler. If you have multiple MATLAB-supported compilers installed on your system, you can change the default using the mex -setup command. See Changing Default Compiler.

Supported Compilers for Simulation Builds.  To view a list of compilers for building models containing MATLAB Function blocks for simulation:

  1. Navigate to the Supported and Compatible Compilers Web page.

  2. Select your platform.

  3. In the table for Simulink and related products, find the compilers checked in the column titled Simulink for MATLAB Function blocks.

Supported Compilers for Code Generation.  To generate code for models that contain MATLAB Function blocks, you can use any of the C compilers supported by Simulink software for code generation with Simulink Coder™. For a list of these compilers:

  1. Navigate to the Supported and Compatible Compilers Web page.

  2. Select your platform.

  3. In the table for Simulink and related products, find the compilers checked in the column titled Simulink Coder.

How to Generate Code for the MATLAB Function Block

  1. Open the call_stats_block2 model that you saved at the end of Programming the MATLAB Function Block.

  2. Double-click its MATLAB Function block stats to open it for editing.

  3. In the MATLAB Function Block Editor, select Build Model > Build to compile and build the example model.

    If no errors occur, the Simulation Diagnostics window displays a message indicating success. Otherwise, this window helps you locate errors, as described in How to Locate and Fix Errors.

How to Locate and Fix Errors

If errors occur during the build process, the Simulation Diagnostics window lists the errors with links to the offending code.

The following exercise shows how to locate and fix an error in a MATLAB Function block.

  1. In the stats function, change the local function avg to a fictitious local function aug and then compile again to see the following messages in window:

    The Simulation Diagnostics window displays each detected error with a red button.

  2. Click the first error line to display its diagnostic message in the bottom error window.

    The message also links to a report about compile-time type information for variables and expressions in your MATLAB functions. This information helps you diagnose error messages and understand type propagation rules. For more information about the report, see MATLAB Function Reports.

  3. In the diagnostic message for the selected error, click the blue link after the function name to display the offending code.

    The offending line appears highlighted in the MATLAB Function Block editor:

  4. Correct the error by changing aug back to avg and recompile.

Defining Inputs and Outputs

In the stats function header for the MATLAB Function block you defined in Programming the MATLAB Function Block, the function argument vals is an input, and mean and stdev are outputs. By default, function inputs and outputs inherit their data type and size from the signals attached to their ports. In this topic, you examine input and output data for the MATLAB Function block to verify that it inherits the correct type and size.

  1. Open the call_stats_block2 model that you saved at the end of Programming the MATLAB Function Block. Double-click the MATLAB Function block stats to open it for editing.

  2. In the MATLAB Function Block editor, select Edit Data.

    The Ports and Data Manager opens to help you define arguments for MATLAB Function blocks.

    The left pane displays the argument vals and the return values mean and stdev that you have already created for the MATLAB Function block. Notice that vals is assigned a Scope of Input, which is short for Input from Simulink. mean and stdev are assigned the Scope of Output, which is short for Output to Simulink.

  3. In the left pane of the Ports and Data Manager, click anywhere in the row for vals to highlight it.

    The right pane displays the Data properties dialog box for vals. By default, the class, size, and complexity of input and output arguments are inherited from the signals attached to each input or output port. Inheritance is specified by setting Size to -1, Complexity to Inherited, and Type to Inherit: Same as Simulink.

    The actual inherited values for size and type are set during compilation of the model, and are reported in the Compiled Type and Compiled Size columns of the left pane.

    You can specify the type of an input or output argument by selecting a type in the Type field of the Data properties dialog box, for example, double. You can also specify the size of an input or output argument by entering an expression in the Size field. For example, you can enter [2 3] in the Size field to specify vals as a 2-by-3 matrix. See Type Function Arguments and Size Function Arguments for more information on the expressions that you can enter for type and size.

      Note:   The default first index for any arrays that you add to a MATLAB Function block function is 1, just as it would be in MATLAB.

For more information, see Ports and Data Manager.

Was this topic helpful?