# lasterr

## Purpose

Last error message.

## Synopsis

    str = lasterr
    lasterr('')

## Description

`str = lasterr` returns the last error message generated by MATLAB.

`lasterr('')` resets the `lasterr` function so it returns an empty matrix until the next error occurs.

`lasterr` is useful in conjunction with the two argument form of `eval`:

    eval('str1','str2')

where `str2` examines the `lasterr` string to determine the cause of the error and take appropriate action.

## Example

Here is an example of a function that examines the `lasterr` string and displays its own message based on the error that last occurred. This example deals with two cases, each of which are errors that can result from a matrix multiply.

    function catch
    l = lasterr;
    j = findstr(l,'Inner matrix dimensions');
    if j~=[]
        disp('Wrong dimensions for matrix multiply')
    else
        k = findstr(l,'Undefined function or variable')
        if (k~=[])
            disp('At least one operand does not exist')
        end
    end

The two argument form of the `eval` function,

    eval('str1','str2')

evaluates the string `str1` and returns if no error occurs. If an error does occur, `eval` executes `str2`. Try using this form of `eval` with the `catch` function shown above:

    clear
    A = [1  2  3; 6  7  2; 0  -1  5];
    B = [9  5  6; 0  4  9];
    eval('A*B','catch')

MATLAB responds with

    Wrong dimensions for matrix multiply

## Example

    lcm(8,40)

    ans =
        40

## See Also

    error, eval

(c) Copyright 1994 by The MathWorks, Inc.