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.
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
Enclose the code in the
myProc:= proc(n) begin if n = 1 or n = 0 then 1 else n * myProc(n - 1) end_if; end_proc:
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
If you just want to display the results, and do not
need to use them in further computations, use the
myProcPrint:= proc(n) begin print(n); if n = 0 or n = 1 then return(1); end_if; n * myProcPrint(n - 1); end_proc:
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) begin if a < b then [a, b] else [b, a] end_if; end_proc:
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
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.
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.
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
The function call
read(filename) searches for the
program file in this order:
Folders specified by the environment variable
filename regarded as an absolute
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.
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.
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
For example, save the procedure as
Return to the MATLAB Command Window. Before calling the procedure at the MATLAB command line, enter:
read command reads
and executes the
myProcedure.mu 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 = 1307674368000
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
feval to call
reverted. To pass a character vector
argument to the procedure, use double quotes inside single quotes.
feval(symengine, 'reverted', '"Abccba"')
ans = 1