This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Generate MATLAB Function Blocks from Symbolic Expressions

Using matlabFunctionBlock, you can generate a MATLAB® Function block. The generated block is available for use in Simulink® models, whether or not the computer running the simulations has a license for Symbolic Math Toolbox™.

If you work in the MuPAD® Notebook app, see Create MATLAB Function Blocks from MuPAD Expressions.

Generate and Edit a Block

Suppose, you want to create a model involving the symbolic expression r = sqrt(x^2 + y^2). Before you can convert a symbolic expression to a MATLAB Function block, create an empty model or open an existing one:


Create a symbolic expression and pass it to the matlabFunctionBlock command. Also specify the block name:

syms x y
r = sqrt(x^2 + y^2);
matlabFunctionBlock('my_system/my_block', r)

If you use the name of an existing block, the matlabFunctionBlock command replaces the definition of an existing block with the converted symbolic expression.

You can open and edit the generated block. To open a block, double-click it.

function r = my_block(x,y)

r = sqrt(x.^2+y.^2);

    Tip   Some symbolic expressions cannot be represented using MATLAB functions. matlabFunctionBlock cannot convert these symbolic expressions, but issues a warning. Since these expressions might result in undefined function calls, always check conversion results and verify results by running the simulation containing the resulting block.

Control the Order of Input Ports

matlabFunctionBlock generates input variables and the corresponding input ports in alphabetical order from a symbolic expression. To change the order of input variables, use the vars option:

syms x y
mu = sym('mu');
dydt = -x - mu*y*(x^2 - 1);
matlabFunctionBlock('my_system/vdp', dydt,'vars', [y mu x])

Name the Output Ports

By default, matlabFunctionBlock generates the names of the output ports as the word out followed by the output port number, for example, out3. The output option allows you to use the custom names of the output ports:

syms x y
mu = sym('mu');
dydt = -x - mu*y*(x^2 - 1);
matlabFunctionBlock('my_system/vdp', dydt,'outputs',{'name1'})
Was this topic helpful?