Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Write Error and Warning Messages

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.

When writing a procedure in MuPAD®, you can include error and warning messages in your procedure. If the system encounters an error while executing a procedure, it terminates execution of the procedure and displays an error message. If the system encounters a warning, it prints the warning message and continues executing the procedure. You can specify your own error and warning messages.

The error function terminates execution of a current procedure with an error. For example, the following procedure converts the number of a month to the name of a month. The procedure checks the type of its argument and accepts only positive integer numbers. Since there are only 12 months in a year, the procedure must also ensure that the number does not exceed 12. The error function lets you terminate the procedure call with an appropriate error message if the number is greater than 12:

monthNumberToName := proc(n:Type::PosInt)
begin
  if n > 12 then
   error("Invalid number. The number must not exceed 12.")
  end_if;
  case n
    of 1 do return(January)
    of 2 do return(February)
    of 3 do return(March)
    of 4 do return(April)
    of 5 do return(May)
    of 6 do return(June)
    of 7 do return(July)
    of 8 do return(August)
    of 9 do return(September)
    of 10 do return(October)
    of 11 do return(November)
    of 12 do return(December)
  end_case:
end:

If you call monthNumberToName with any integer from 1 to 12, the procedure returns the name of a month:

monthNumberToName(12)

If you call monthNumberToName with an integer greater than 12, the procedure terminates with the specified error:

monthNumberToName(13)
Error: Invalid number. The number must not exceed 12. [monthNumberToName]

Warning messages help you inform your users about potential problems in the algorithm. These problems are typically minor and do not interfere with the execution of a procedure. For example, you can warn your users about limited functionality of a procedure or about implicit assumptions made by a procedure.

The following procedure uses the simplify function to simplify the fraction. The function implicitly assumes that the variable x is not equal to a. If your procedure uses the simplify function, you can add the following warning for your users:

simplifyW := proc(a)
begin
  warning("Assuming x <> ".expr2text(a));
  simplify((x^2 - a^2)/(x - a))
end:

Now, your procedure informs its users about the assumption:

simplifyW(10)
Warning: Assuming x <> 10 [simplifyW]

Was this topic helpful?