|On this page…|
You can issue a warning to flag unexpected conditions detected when running a program. The warning function prints a warning message to the command line. Warnings differ from errors in two significant ways:
Warnings do not halt the execution of the program.
You can suppress any unhelpful MATLAB® warnings.
Use the warning function in your code to generate a warning message during execution. Specify the message string as the input argument to the warning function:
warning('Input must be a string')
For example, you can insert a warning in your code to verify the software version:
function warningExample1 if ~strncmp(version, '7', 1) warning('You are using a version other than v7') end
You can throw an error to flag fatal problems within the program. Use the error function to print error messages to the command line. After displaying the message, MATLAB stops the execution of the current program.
For example, suppose you construct a function that returns the number of combinations of k elements from n elements. Such a function is nonsensical if k > n; you cannot choose 8 elements if you start with just 4. You must incorporate this fact into the function to let anyone using combinations know of the problem:
function com = combinations(n,k) if k > n error('Cannot calculate with given values') end com = factorial(n)/(factorial(k)*factorial(n-k)); end
If the combinations function receives invalid input, MATLAB stops execution immediately after throwing the error message:
Error using combinations (line 3) Cannot calculate with given values
To make your warning or error messages more specific, insert components of the message string at the time of execution. The warning function uses conversion characters that are the same as those used by the sprintf function. Conversion characters act as placeholders for substrings or values, unknown until the code executes.
For example, this warning uses %s and %d to mark where to insert the values of variables arrayname and arraydims:
warning('Array %s has %d dimensions.',arrayname,arraydims)
If you execute this command with arrayname = 'A' and arraydims = 3, MATLAB responds:
Warning: Array A has 3 dimensions.
Adding run-time parameters to your warnings and errors can clarify the problems within a program. Consider the function combinations from Throw Errors. You can throw a much more informative error using run-time parameters:
function com = combinations(n,k) if k > n error('Cannot choose %i from %i elements',k,n) end com = factorial(n)/(factorial(k)*factorial(n-k)); end
If this function receives invalid arguments, MATLAB throws an error message and stops the program:
Error using combinations (line 3) Cannot choose 9 from 6 elements
A message identifier provides a way to uniquely reference a warning or an error.
Enable or disable warnings with identifiers. Use an identifying string argument with the warning function to attach a unique tag to a message:
For example, you can add an identifier tag to the previous MATLAB warning about which version of software is running:
minver = '7'; if ~strncmp(version,minver,1) warning('MYTEST:VERCHK','Running a version other than v%s',minver) end
Adding an identifier to an error message allows for negative testing. However, adding and recovering more information from errors often requires working with MException objects.