Documentation

Create MATLAB Function Blocks from MuPAD Expressions

Symbolic Math Toolbox™ lets you create a MATLAB® function block from a symbolic expression. The generated block is available for use in Simulink® models, whether or not the computer that runs the simulations has a license for Symbolic Math Toolbox.

If you work in the MATLAB Command Window, see Generate MATLAB Function Blocks.

The MuPAD® Notebook app does not provide a function for generating a block. Therefore, to be able to create a block from the MuPAD expression:

  1. In a MuPAD notebook, assign that expression to a variable.

  2. Use the getVar function to copy that variable from a notebook to the MATLAB workspace.

For details about these steps, see Copy MuPAD Variables to the MATLAB Workspace.

When the expression that you want to use for creating a MATLAB function block appears in the MATLAB workspace, use the matlabFunctionBlock function to create a block from that expression.

For example, open a MuPAD notebook with the handle notebook_handle:

notebook_handle = mupad;

In this notebook, create the following symbolic expression:

r := sqrt(x^2 + y^2)

Use getVar to copy variable r to the MATLAB workspace:

r = getVar(notebook_handle,'r')

Variable r and its value appear in the MATLAB workspace and in the MATLAB Command Window:

r =
(x^2 + y^2)^(1/2)

Before generating a MATLAB Function block from the expression, create an empty model or open an existing one. For example, create and open the new model my_system:

new_system('my_system')
open_system('my_system')

Since the variable and its value are in the MATLAB workspace, you can use matlabFunctionBlock to generate the block my_block:

matlabFunctionBlock('my_system/my_block', r)

You can open and edit the block in the MATLAB Editor. To open the block, double-click it:

function r = my_block(x,y)
%#codegen

r = sqrt(x.^2+y.^2);
Was this topic helpful?