|Programming and Data Types|
MATLAB gives you the ability to control what happens when a warning is encountered during M-file program execution. Options that are available include
Depending on how you set up your warning controls, you can have these actions affect all warnings in your code, specific warnings that you select, or just the most recently invoked warning.
Setting up this system of warning control involves several steps.
The warning statements that you put into your M-file code must contain the string that is to be displayed when the warning is incurred, and may also contain a message identifier. If you are not planning to use warning control or if you have no need to single out certain warnings for control, then you only need to specify the message string. Use the syntax shown in the section on Warnings. Valid formats are
Attaching an Identifier to the Warning Statement. If there are specific warnings that you want MATLAB to be able to apply control statements to, then you need to include a message identifier in the warning statement. The message identifier must be the first argument in the statement. Valid formats are
See Message Identifiers for information on how to specify the
When you specify more than one input argument with |
Warning Control Statements
Once you have the warning statements in your M-file code and are ready to execute the code, you can indicate how you want MATLAB to act on these warnings by issuing control statements. These statements place the specified warning(s) into a desired state and have the format
Control statements can also return information on the state of selected warnings. This only happens if you assign the output to a variable, as shown below. See Output from Control Statements.
Warning States. There are three possible values for the
state argument of a warning control statement.
||Enable the display of selected warning message.
||Disable the display of selected warning message.
||Display the current state of selected warning.
Message Identifiers. In addition to the message identifiers already discussed, there are two other identifiers that you can use in control statements only.
||Set selected warning to the specified state.
||Set all warnings to the specified state.
||Set only the last displayed warning to the specified state.
Example 1. Enable just the
actionNotTaken warning from Simulink by first turning off all warnings and then setting just that warning to
query to determine the current state of all warnings. It reports that you have set all warnings to
off with the exception of Simulink:actionNotTaken.
Example 2. Evaluating
inv on zero displays a warning message. Turn off the most recently invoked warning with
warning off last.
Debug, Backtrace, and Verbose Modes
In addition to warning messages, there are three modes that can be enabled or disabled with a warning control statement. These modes are shown here.
||Stop in the debugger when a warning is invoked.
||Display an M-stack trace after a warning is invoked.
||Display a message on how to suppress the warning.
The syntax for using this type of control statement is as follows, where
state, in this case, can be only
Note that there is no need to include a message identifier with this type of control statement. All enabled warnings are affected by the this type of control statement.
Example 1. To enter debug mode whenever a Simulink
actionNotTaken warning is invoked, first turn off all warnings and enable only this one type of warning using its message identifier. Then turn on debug mode for all enabled warnings. When you run your program, MATLAB will stop in debug mode just before this warning is executed. You will see the debug prompt (
Example 2. By default, there is an extra line of information shown with each warning telling you how to suppress it.
Disable this extra line by turning off
Output from Control Statements
When you include an output variable in a control statement,
warning returns a MATLAB structure array containing the state of the selected warning and assigns it to this variable. You must type the command using the MATLAB function format, that is, parentheses and quotation marks are required.
MATLAB does not display |
You can use output variables with any type of control statement. If you just want to collect the information but don't want to change state, then simply perform a
query on the warning(s). MATLAB returns the current state of those warnings selected by the message identifier.
If you want to change state, but also save the former state so that you can restore it later, use the return structure array to save that state. The following example does an implicit
query, returning state information in
s, and then turns on all warnings.
See the section, Saving and Restoring State, for more information on restoring the former state of warnings.
Output Structure Array. Each element of the structure array returned by
warning contains two fields.
||Message identifier string,
||State of warning(s) prior to invoking this control statement
If you query for the state of just one warning, using a message identifier or
'last' in the command, then MATLAB returns a one-element structure array. The
identifier field contains the selected message identifier and the
state field holds the current state of that warning.
If you query for the state of all warnings, using
'all' in the command, MATLAB returns a structure array having one or more elements.
warning on|off allcommand.)
warning off all warning on MATLAB:divideByZero warning on MATLAB:fileNotFound s = warning('query', 'all') s = 3x1 struct array with fields: identifier state s(1) ans = identifier: 'all' state: 'off' s(2) ans = identifier: 'MATLAB:divideByZero' state: 'on' s(3) ans = identifier: 'MATLAB:fileNotFound' state: 'on'
Saving and Restoring State
If you want to temporarily change the state of some warnings and then later return to your original settings, you can save the original state in a structure array and then restore it from that array. You can save and restore the state of all of your warnings or just one that you select with a message identifier.
To save the current warning state, assign the output of a warning control statement, as discussed in the last section, Output from Control Statements. The following statement saves the current state of all warnings in structure array
To restore state from
s, use the syntax shown below. Note that the MATLAB function format (enclosing arguments in parentheses) is required.
Example 1. Perform a query of all warnings to save the current state in structure array
Then, after doing some work that includes making changes to the state of some warnings, restore the original state of all warnings.
Example 2. Turn on one particular warning, saving the previous state of this warning in
s. Remember that this nonquery syntax (where
off) performs an implicit query prior to setting the new state.
Restore the state of that one warning when you are ready, with
|Using Message Identifiers with lasterr||Debugging Errors and Warnings|