Create 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 Generate MATLAB Functions.

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:

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.

Copy MuPAD Variables to the MATLAB Workspace

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:

  1. 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)
  2. 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.

Generate MATLAB Code in a MuPAD Notebook

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.

  1. In the MuPAD Notebook app, 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)):

      Note:   If the file with this name already exists, fprint replaces the contents of this file with the converted expression.

  2. 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);
  3. 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:

Was this topic helpful?