Programming and Data Types | ![]() ![]() |
Displaying Error and Warning Messages
Use the error
and fprintf
functions to display error information on the screen. The error
function has the syntax
error('error string
')
If you call the error
function from inside an M-file, error
displays the text in the quoted string and causes the M-file to stop executing. For example, suppose the following appears inside the M-file myfile.m
.
if n < 1 error('n must be 1 or greater.') end
For n
equal to 0
, the following text appears on the screen and the M-file stops.
??? Error using ==> myfile
n must be 1 or greater.
In MATLAB, warnings are similar to error messages, except program execution does not stop. Use the warning
function to display warning messages.
warning('warning string
')
The function lastwarn
displays the last warning message issued by MATLAB.
![]() | Errors and Warnings | Dates and Times | ![]() |