| Contents | Index |
| On this page… |
|---|
Creating MATLAB Functions from MuPAD Expressions |
Symbolic Math Toolbox lets you create a MATLAB function from a symbolic expression. A MATLAB function created from a symbolic expression accepts numeric arguments and evaluates the expression applied to the arguments. You can generate a function handle or a file that contains a MATLAB function. The generated file is available for use in any MATLAB calculation, independent of a license for Symbolic Math Toolbox functions.
If you work in the MATLAB Command Window, see Generating MATLAB Functions.
When you use the MuPAD notebook interface, all your symbolic expressions are written in the MuPAD language. To be able to create a MATLAB function from such expressions, you must convert it to the MATLAB language. There are two approaches for converting a MuPAD expression to the MATLAB language:
Assign the MuPAD expression to a variable, and copy that variable from a notebook to the MATLAB workspace. This approach lets you create a function handle or a file that contains a MATLAB function. It also requires using a handle to the notebook.
Generate MATLAB code from the MuPAD expression in a notebook. This approach limits your options to creating a file. You can skip creating a handle to the notebook.
The generated MATLAB function can depend on the approach that you chose. For example, code can be optimized differently or not optimized at all.
Suppose you want to create a MATLAB function from a symbolic matrix that converts spherical coordinates of any point to its Cartesian coordinates. First, open a MuPAD notebook with the handle notebook_handle:
notebook_handle = mupad;
In this notebook, create the symbolic matrix S that converts spherical coordinates to Cartesian coordinates:
x := r*sin(a)*cos(b): y := r*sin(a)*sin(b): z := r*cos(b): S := matrix([x, y, z]):
Now convert matrix S to the MATLAB language. Choose the best approach for your task.
If your notebook has a handle, like notebook_handle in this example, you can copy variables from that notebook to the MATLAB workspace with the getVar function, and then create a MATLAB function. For example, to convert the symbolic matrix S to a MATLAB function:
Copy variable S to the MATLAB workspace:
S = getVar(notebook_handle,'S')
Variable S and its value (the symbolic matrix) appear in the MATLAB workspace and in the MATLAB Command Window:
S =
r*cos(b)*sin(a)
r*sin(a)*sin(b)
r*cos(b)Use matlabFunction to create a MATLAB function from the symbolic matrix. To generate a MATLAB function handle, use matlabFunction without additional parameters:
h = matlabFunction(S)
h =
@(a,b,r)[r.*cos(b).*sin(a);r.*sin(a).*sin(b);r.*cos(b)]To generate a file containing the MATLAB function, use the parameter file and specify the path to the file and its name. For example, save the MATLAB function to the file cartesian.m in the current folder:
S = matlabFunction(S,'file', 'cartesian.m');
You can open and edit cartesian.m in the MATLAB Editor.

To generate the MATLAB code from a MuPAD expression within the MuPAD notebook, use the generate::MATLAB function. Then, you can create a new file that contains an empty MATLAB function, copy the code, and paste it there. Alternatively, you can create a file with a MATLAB formatted string representing a MuPAD expression, and then add appropriate syntax to create a valid MATLAB function.
In the MuPAD notebook interface, use the generate::MATLAB function to generate MATLAB code from the MuPAD expression. Instead of printing the result on screen, use the fprint function to create a file and write the generated code to that file:
fprint(Unquoted, Text, "cartesian.m", generate::MATLAB(S)):
Open cartesian.m. It contains a MATLAB formatted string representing matrix S:
S = zeros(3,1); S(1,1) = r*cos(b)*sin(a); S(2,1) = r*sin(a)*sin(b); S(3,1) = r*cos(b);
To convert this file to a valid MATLAB function, add the keywords function and end, the function name (must match the file name), input and output arguments, and comments:

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 Generating MATLAB Function Blocks.
The MuPAD notebook interface does not provide a function for generating a block. Therefore, to be able to create a block from the MuPAD expression:
In a MuPAD notebook, assign that expression to a variable.
Use the getVar function to copy that variable from a notebook to the MATLAB workspace.
For details about these steps, see Copying 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);
Symbolic Math Toolbox lets you integrate symbolic computations into the Simscape modeling workflow by using the results of these computations in the Simscape equation section.
If you work in the MATLAB Command Window, see Generating Simscape Equations.
If you work in the MuPAD notebook interface, you can:
Assign the MuPAD expression to a variable, copy that variable from a notebook to the MATLAB workspace, and use simscapeEquation to generate the Simscape equation in the MATLAB Command Window.
Generate the Simscape equation from the MuPAD expression in a notebook.
In both cases, to use the generated equation, you must manually copy the equation and paste it to the equation section of the Simscape component file.
For example, follow these steps to generate a Simscape equation from the solution of the ordinary differential equation computed in the notebook interface:
Open a MuPAD notebook with the handle notebook_handle:
notebook_handle = mupad;
In this notebook, define the following equation:
s:= ode(y'(t) = y(t)^2, y(t)):
Decide whether you want to generate the Simscape equation in the MuPAD notebook interface or in the MATLAB Command Window.
To generate the Simscape equation in the same notebook, use generate::Simscape. To display generated Simscape code on screen, use the print function. To remove quotes and expand special characters like line breaks and tabs, use the printing option Unquoted:
print(Unquoted, generate::Simscape(s))
This command returns the Simscape equation that you can copy and paste to the Simscape equation section:
-y^2+y.der == 0.0;
To generate the Simscape equation in the MATLAB Command Window, follow these steps:
Use getVar to copy variable s to the MATLAB workspace:
s = getVar(notebook_handle, 's')
Variable s and its value appear in the MATLAB workspace and in the MATLAB Command Window:
s = ode(D(y)(t) - y(t)^2, y(t))
Use simscapeEquation to generate the Simscape equation from s:
simscapeEquation(s)
You can copy and paste the generated equation to the Simscape equation section. Do not copy the automatically generated variable ans and the equal sign that follows it.
ans = s == (-y^2+y.der == 0.0);
![]() | Integration of MuPAD and MATLAB | Function Reference | ![]() |

See how symbolic computations can help you find analytical solutions to math and engineering problems.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |