Note: MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
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 Live Editor, see Generate MATLAB Functions from Symbolic Expressions.
When you use the MuPAD Notebook app, 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 = mupad;
In this notebook, create the symbolic matrix
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
this example, you can copy variables from that notebook to the MATLAB workspace
getVar function, and then create a MATLAB function.
For example, to convert the symbolic matrix
a MATLAB function:
S to the MATLAB workspace:
S = getVar(notebook_handle,'S')
S and its value (the symbolic matrix)
appear in the MATLAB workspace and in the MATLAB Live Editor:
S = r*cos(b)*sin(a) r*sin(a)*sin(b) r*cos(b)
matlabFunction to create a MATLAB function
from the symbolic matrix. To generate a MATLAB function handle,
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
file and specify the path to the
file and its name. For example, save the MATLAB function to the
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
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 character vector representing a MuPAD expression,
and then add appropriate syntax to create a valid MATLAB function.
In the MuPAD Notebook app, use the
to generate MATLAB code from the MuPAD expression. Instead
of printing the result on screen, use the
to create a file and write the generated code to that file:
fprint(Unquoted, Text, "cartesian.m", generate::MATLAB(S)):
If the file with this name already exists,
cartesian.m. It contains a MATLAB formatted
character vector representing matrix
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
the function name (must match the file name), input and output arguments,