Documentation

## History Mechanism

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.

### Access the History Table

MuPAD® implements the internal history mechanism. The history mechanism lets you access a limited number of previously computed results, with or without the commands that generated the results. The history mechanism also helps you reduce the number of additional identifiers commonly used for storing the results of intermediate computations. Instead of assigning results of such computations to auxiliary identifiers, you can access the entries of the history table and get the previously computed result.

To access the entries of the history table, use the `last` and `history` functions. The `last` function returns previously computed results without the command that generated the results. The `history` function returns the previously computed results along with the commands that generated those results.

For the `last` function, MuPAD also provides the shortcut `%`. The function call `last(n)` (or ```% n```) returns the `n`th entry of the history table. The `last` function counts the entries of the history table from the end of the table. Thus, when you use the `last` function, the most recent result is the first entry of the history table.

For example, compute the factorials of the numbers 10, 20, and 30:

`10!; 20!; 30!;`

To access the computed factorial of 30, use the function call `last`(3) or the shorter call `%`3:

`last(3)`

### Note

When you call the `last` or `history` function, MuPAD adds the result of that call to the history table.

Calling the `last` function or its shortcut `%` inserts a new entry in the history table. Thus, the history table now contains the results of the following evaluations: 10!, 20!, 30!, and %3 (which in this example is equal to 10!). If you call the `last` function with the argument 3 again, the system displays the result of evaluation of 20!:

`last(3)`

To access the most recent entry of the history table, you can use the shortcut `%` without parameters. For example, solve the following equation, and then simplify the result. Note that using `%` lets you avoid assigning the result of a solution to an identifier:

`solve(log(2, x) + log(2, 5) = x + 5, x);`

`simplify(%)`

The `last` function does not evaluate results. The `last` function also returns the results for which you used colons to suppress the outputs:

```hold(2 + 2): %```

The `history` function displays both the result and the command that produced that result. This function counts the entries of the history table from the first result obtained in the current MuPAD session. Thus, when you use the `history` function, the most recent result is the last entry of the history table. In this section, the first entry of the history table is the computation of the factorial of 10:

`history(1)`

To find the current number of entries in the history table, call the `history` function without an argument:

`history()`

You can use the `history` function to access the most recent computation and its result:

```a := 2: history(history())```

For the following statements, the history mechanism depends on whether you call the statement interactively or within a procedure:

These statements are called compound statements. At the interactive level, MuPAD stores compound statements as one unit. In procedures, MuPAD stores the statements found within a compound statement in a separate history table of the procedure. In this case, the system does not store the compound statement itself.

### Specify Maximum Number of Entries

By default, the history table for interactive computations can contain up to 20 entries. When the number of entries reaches 20, the system removes the oldest entry from the history table every time it needs to add a new entry. For interactive computations, the environment variable `HISTORY` determines the maximum number of entries in the history table:

`HISTORY`

To change the maximum number of entries in the history table for the current MuPAD session, assign the new value to `HISTORY`:

`HISTORY := 2:`

Now MuPAD stores only the two most recent commands and their results in the history table:

```a := 1: b := 2: c := 3: %1, %2; %3```

```Error: Invalid argument. [last] ```

### Note

Within a procedure, the maximum number of entries in the local history table of the procedure is always 3, independent of the value of `HISTORY`.

For further computations, restore the default maximum number entries in the history table:

```delete HISTORY: HISTORY```

### Clear the History Table

Although MuPAD restricts the number of entries in the history table, it does not restrict its size. If a command returns a large result, the system stores that result in the history table. While this result remains in the history table, MuPAD cannot release the memory needed to store that result. One or more large entries in the history table can significantly reduce available memory and slow down further computations. If you know that a particular command returns memory-consuming results, avoid writing that command and its results to the history table. To avoid writing a command and its result to the history table, set the value of `HISTORY` to 0. The disadvantage of this approach is that you delete all previous results from the history table.

For example, set the value of `HISTORY` to 0 before creating a sequence of 1,000,000 random numbers:

```HISTORY := 0: random() \$ i = 1..10^6:```

For further computations, restore the default maximum number entries in the history table:

```delete HISTORY: HISTORY```

If the history table already contains a memory-consuming result, to release the memory you also can clear the history table by setting the value of `HISTORY` to 0. Alternatively, you can wait until the MuPAD fills the history table with new entries. Also, you can select Notebook > Disconnect to restart the MuPAD engine.

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos