# Documentation

## Generate MATLAB Function Blocks

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:

```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);```

### 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'})```

You can convert a MuPAD expression or function to a MATLAB Function block:

```syms x y 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);```
 Tip   Some MuPAD expressions cannot be correctly converted 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 results of conversion. To verify the results, you can run the simulation containing the resulting block.