Use Your Own MuPAD Procedures


MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

To convert a MuPAD notebook file to a MATLAB live script file, see convertMuPADNotebook. MATLAB live scripts support most MuPAD functionality, although there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Write MuPAD Procedures

A MuPAD procedure is a text file that you can write in any text editor. The recommended practice is to use the MATLAB Editor.

To define a procedure, use the proc function. Enclose the code in the begin and end_proc functions:

myProc:= proc(n)
   if n = 1 or n = 0 then
     n * myProc(n - 1)

By default, a MuPAD procedure returns the result of the last executed command. You can force a procedure to return another result by using return. In both cases, a procedure returns only one result. To get multiple results from a procedure, combine them into a list or other data structure, or use the print function.

  • If you just want to display the results, and do not need to use them in further computations, use the print function. With print, your procedure still returns one result, but prints intermediate results on screen. For example, this procedure prints the value of its argument in each call:

    myProcPrint:= proc(n)
       if n = 0 or n = 1 then
       n * myProcPrint(n - 1);
  • If you want to use multiple results of a procedure, use ordered data structures, such as lists or matrices as return values. In this case, the result of the last executed command is technically one object, but it can contain more than one value. For example, this procedure returns the list of two entries:

    myProcSort:= proc(a, b)
      if a < b then
        [a, b]
        [b, a]

    Avoid using unordered data structures, such as sequences and sets, to return multiple results of a procedure. The order of the entries in these structures can change unpredictably.

When you save the procedure, it is recommended to use the extension .mu. For details, see Notebook Files and Program Files. The name of the file can differ from the name of the procedure. Also, you can save multiple procedures in one file.

Steps to Take Before Calling a Procedure

To be able to call a procedure, you must first execute the code defining that procedure, in a notebook. If you write a procedure in the same notebook, simply evaluate the input region that contains the procedure. If you write a procedure in a separate file, you must read the file into a notebook. Reading a file means finding it and executing the commands inside it.

Read MuPAD Procedures

If you work in the MuPAD Notebook and create a separate program file that contains a procedure, use one of the following methods to execute the procedure in a notebook. The first approach is to select Notebook > Read Commands from the main menu.

Alternatively, you can use the read function. The function call read(filename) searches for the program file in this order:

  1. Folders specified by the environment variable READPATH

  2. filename regarded as an absolute path

  3. Current folder (depends on the operating system)

If you want to call the procedure from the MATLAB Live Editor, you still need to execute that procedure before calling it. See Call Your Own MuPAD Procedures.

Use Startup Commands and Scripts

Alternatively, you can add a MuPAD procedure to startup commands of a particular notebook. This method lets you execute the procedure every time you start a notebook engine. Startup commands are executed silently, without any visible outputs in the notebook. You can copy the procedure to the dialog box that specifies startup commands or attach the procedure as a startup script. For information, see Hide Code Lines.

Call Your Own MuPAD Procedures

You can extend the functionality available in the toolbox by writing your own procedures in the MuPAD language. This section explains how to call such procedures at the MATLAB Command Window.

Suppose you wrote the myProc procedure that computes the factorial of a nonnegative integer.

Save the procedure as a file with the extension .mu. For example, save the procedure as in the folder C:/MuPAD.

Return to the MATLAB Command Window. Before calling the procedure at the MATLAB command line, enter:

read(symengine, 'C:/MuPAD/')

The read command reads and executes the file in MuPAD. After that, you can call the myProc procedure with any valid parameter. For example, compute the factorial of 15:

feval(symengine, 'myProc', 15)
ans =

If your MuPAD procedure accepts character vector arguments, enclose these arguments in two sets of quotes: double quotes inside single quotes. Single quotes suppress evaluation of the argument before passing it to the MuPAD procedure, and double quotes let MuPAD recognize that the argument is a character vector. For example, this MuPAD procedure converts a character vector to lowercase and checks if reverting that character vector changes it.

In the MATLAB Command Window, use the read command to read and execute

read(symengine, 'C:/MuPAD/')

Now, use feval to call the procedure reverted. To pass a character vector argument to the procedure, use double quotes inside single quotes.

feval(symengine, 'reverted', '"Abccba"')
ans =