Debug MuPAD Code in the Tracing Mode

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

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

MuPAD® provides two alternatives for debugging your code. First, you can use the MuPAD GUI debugger to debug your code interactively, observing execution of the code step by step. Another alternative is to generate a report showing the steps that were taken while executing your code. The mode in which MuPAD generates a report while executing a procedure, domain, method, or function environment is called the tracing mode. This mode is helpful for multistep procedures that require a long time to execute. Use this approach to debug a particular procedure, domain, method, or function environment, for which you want to print a report.

When debugging, you also can use the print and fprint functions for printing intermediate results produced by your code.

Suppose, you want to create a procedure that computes the Lucas numbers. The Lucas numbers are a sequence of integers. The recursion formula that defines the nth Lucas number is similar to the definition of the Fibonacci numbers:

Although MuPAD does not provide a function that computes the Lucas numbers, writing your own procedure for this task is easy:

lucas:= proc(n:Type::PosInt)
begin
  if n = 1 then
    1
  elif n = 2 then
    3
  else
    lucas(n - 1) + lucas(n - 2)
  end_if
end_proc:

The procedure call lucas(n) returns the nth Lucas number. For example, display the first 10 Lucas numbers:

lucas(n) $ n = 1..10

Suppose you want to trace this procedure. To switch execution of a particular procedure, domain, method, or function environment to the tracing mode, use the prog::trace function. For example, to trace the lucas procedure, enter:

prog::trace(lucas):

Now, if you call the lucas procedure, the trace mechanism observes every step of the procedure call and generates the report for that call:

lucas(5)
enter lucas(5)
  enter lucas(4)
    enter lucas(3)
      enter lucas(2)
      computed 3
      enter lucas(1)
      computed 1
    computed 4
    enter lucas(2)
    computed 3
  computed 7
  enter lucas(3)
    enter lucas(2)
    computed 3
    enter lucas(1)
    computed 1
  computed 4
computed 11

The prog::traced() function call returns the names of all currently traced procedures, domains, methods, and function environments:

prog::traced()

By using different options of the prog::trace function, you can customize generated reports. Most of these options are independent of a particular procedure call. They affect all reports generated after you use an option. See the prog::trace help page for more details.

If a procedure uses many nested procedure calls, the generated report for that procedure can be very long. To limit the number of nested procedure calls in a report, use the Depth option of prog::trace:

prog::trace(Depth = 2):
lucas(5)
enter lucas(5)
  enter lucas(4)
  computed 7
  enter lucas(3)
  computed 4
computed 11

The Depth option affects all reports generated for further calls to procedures, domains, methods, and function environments. If you do not want to use this option for further calls, set its value to 0. The value 0 indicates that prog::trace must display all nested calls:

prog::trace(Depth = 0):

To display memory usage in each step of the procedure call, use the Mem option:

prog::trace(Mem):
lucas(5)
enter lucas(5) [mem: 5636064]
  enter lucas(4) [mem: 5636544]
    enter lucas(3) [mem: 5636944]
      enter lucas(2) [mem: 5637344]
      computed 3 [mem: 5637060]
      enter lucas(1) [mem: 5637424]
      computed 1 [mem: 5637140]
    computed 4 [mem: 5636756]
    enter lucas(2) [mem: 5637120]
    computed 3 [mem: 5636836]
  computed 7 [mem: 5636356]
  enter lucas(3) [mem: 5636720]
    enter lucas(2) [mem: 5637120]
    computed 3 [mem: 5636836]
    enter lucas(1) [mem: 5637200]
    computed 1 [mem: 5636916]
  computed 4 [mem: 5636532]
computed 11 [mem: 5636052]

To stop using the Mem option for further calls, set its value to FALSE:

prog::trace(Mem = FALSE):

To stop tracing calls to the lucas procedure, use the prog::untrace function:

prog::untrace():