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™.
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:
new_system('my_system') open_system('my_system')
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) %#codegen r = sqrt(x.^2+y.^2);
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.
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])
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'})