Documentation

matlabFunctionBlock

Convert symbolic expression to MATLAB Function block

    Note:   emlBlock has been removed. Use matlabFunctionBlock instead.

Syntax

matlabFunctionBlock(block,f)
matlabFunctionBlock(block,f1,...,fN)
matlabFunctionBlock(block,f,Name,Value)
matlabFunctionBlock(block,f1,...,fN,Name,Value)

Description

matlabFunctionBlock(block,f) converts the symbolic expression or function f to a MATLAB® Function block that you can use in Simulink® models. Here, block specifies the name of the block that you create or modify.

matlabFunctionBlock(block,f1,...,fN) converts a vector of the symbolic expressions or functions f1,...,fN to a MATLAB Function block with multiple outputs.

matlabFunctionBlock(block,f,Name,Value) converts the symbolic expression or function f to a MATLAB Function block using additional options specified by one or more Name,Value pair arguments.

matlabFunctionBlock(block,f1,...,fN,Name,Value) converts a vector of the symbolic expressions or functions f to a MATLAB Function block with multiple outputs using additional options specified by one or more Name,Value pair arguments.

Input Arguments

f

Symbolic expression or function.

f1,...,fN

Vector of symbolic expressions or functions.

block

String specifying the block name that you create or modify.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'functionName'

Specify the name of the function. The value must be a string.

Default: The value coincides with the block name.

'outputs'

Specify the names of output ports. The value must be a cell array of strings. The number of entries must equal or exceed the number of free variables in f.

Default: The name of an output port consists of the word out followed by the output port number, for example, out3.

'vars'

Specify the order of the variables and the corresponding input ports of a block. The value must be either a cell array of strings or symbolic arrays, or a vector of symbolic variables. The number of entries must equal or exceed the number of free variables in f.

Default: When converting symbolic expressions, the order is alphabetical. When converting symbolic functions, the input arguments appear in front of other variables. Other variables are sorted alphabetically.

Examples

Before you can convert a symbolic expression to a MATLAB Function block, create an empty model or open an existing one:

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

Use matlabFunctionBlock to create the block my_block containing the symbolic expression:

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

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

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

function f = my_block(x,y,z)
%#codegen

f = x.^2 + y.^2 + z.^2;

Save and close my_system:

save_system('my_system')
close_system('my_system')

Create this symbolic function:

syms x y z
f(x, y, z) = x^2 + y^2 + z^2;

Convert f to a MATLAB Function block:

new_system('my_system')
open_system('my_system')
matlabFunctionBlock('my_system/my_block',f)

Generate a block and set the function name to my_function:

syms x y z
f = x^2 + y^2 + z^2;
new_system('my_system')
open_system('my_system')
matlabFunctionBlock('my_system/my_block', f,...
'functionName', 'my_function')

When generating a block, specify the order of the input variables:

syms x y z
f = x^2 + y^2 + z^2;
new_system('my_system')
open_system('my_system')
matlabFunctionBlock('my_system/my_block', f,...
'vars', [y z x])

When generating a block, rename the output variables and the corresponding ports:

syms x y z
f = x^2 + y^2 + z^2;
new_system('my_system')
open_system('my_system')
matlabFunctionBlock('my_system/my_block', f, f + 1, f + 2,...
'outputs', {'name1','name2','name3'})

Call matlabFunctionBlock using several options simultaneously:

syms x y z
f = x^2 + y^2 + z^2;
new_system('my_system')
open_system('my_system')
matlabFunctionBlock('my_system/my_block', f, f + 1, f + 2,...
'functionName', 'my_function','vars', [y z x],...
'outputs',{'name1','name2','name3'})

Convert this MuPAD® expression to a MATLAB Function block:

syms x y
new_system('my_system')
open_system('my_system')
f = evalin(symengine, 'arcsin(x) + arccos(y)');
matlabFunctionBlock('my_system/my_block', f)

The resulting block contains the same expressions written in the MATLAB language:

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

f = asin(x) + acos(y);

More About

expand all

Tips

  • To convert a MuPAD expression or function to a MATLAB Function block, use f = evalin(symengine,'MuPAD_Expression') or f = feval(symengine,'MuPAD_Function',x1,...,xn). matlabFunctionBlock cannot correctly convert some MuPAD expressions to a block. These expressions do not trigger an error message. When converting a MuPAD expression or function that is not on the MATLAB vs. MuPAD Expressions list, always check the conversion results. To verify the results, you can run the simulation containing the resulting block.

Was this topic helpful?