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) 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 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) 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 .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.
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 app 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:
Folders specified by the environment variable READPATH
filename regarded as an absolute path
Current folder (depends on the operating system)
If you want to call the procedure from the MATLAB Command Window, 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 .mu. For example, save the procedure as myProcedure.mu in the folder C:/MuPAD.
Return to the MATLAB Command Window. Before calling the procedure at the MATLAB command line, enter:
The 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 string 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 string. For example, this MuPAD procedure converts a string to lowercase and checks if reverting that string changes it.
In the MATLAB Command Window, use the read command to read and execute reverted.mu.
Now, use feval to call the procedure reverted. To pass a string argument to the procedure, use double quotes inside single quotes.
feval(symengine, 'reverted', '"Abccba"')
ans = 1