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 MATLAB® code. 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 Author Blocks Using 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 call_stats_block1 model to view the starting blocks. The model includes an empty MATLAB Function block and the blocks you need for the example.

The starting model. The model contains a MATLAB Function block in the center, a Constant block that has a vector with four values to the left, and two Display blocks to the right. The blocks are not connected.

Program the MATLAB Function Block

Program the block to calculate the mean and standard deviation for a vector of values. 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.

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.

On a new line after the function declaration statement, clear the existing code 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;

The final code looks like this:

The MATLAB Function Block Editor showing the final version of the code. It includes the code from the previous steps.

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.

This shows the outline of the block without connections between the blocks.

Complete the connections to the MATLAB Function block.

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

Save the model. To view a completed version of this model, open the call_stats_block2 model.

Check Properties for MATLAB Function Block Variables

You can check and manage properties for variables in MATLAB Function blocks. In this example, verify that the input argument vals inherits its type and size properties. Double-click the MATLAB Function block. Open the Symbols pane and the Property Inspector. In the Function tab, click Edit Data.

The Function tab in the Simulink Editor while the MATLAB Function block Editor is open. The Edit Data button on the left is enclosed in a red box.

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.

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.

The Property Inspector and the Symbols pane. The vals input variable is selected, and the Property Inspector displays the variable properties.

For more information on creating variables in the MATLAB Function block and setting properties, see Create and Define MATLAB Function Block Variables.

Simulate the Model

In Simulink, click Run to simulate the model. The model outputs the data from the function to the two Display blocks.

The Display blocks show the simulation results of the model. The first Display block shows 3.5 and the second shows 1.118.

The block also plots the input data in a separate window.

A plot of the input values and a line drawn through the values. The line is blue.

See Also

| |

Related Topics

External Websites