Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

intwarning - Control state of integer warnings

Syntax

intwarning('action')
s = intwarning('action')
intwarning(s)
sOld = intwarning(sNew)

Description

The MATLAB software offers four types of warnings on invalid operations that involve integers. The intwarning function enables, disables, or returns information on these warnings:

intwarning('action') sets or displays the state of integer warnings in MATLAB according to the string, action. There are three possible actions, as shown here. The default state is 'off'.

Action

Description

off

Disable the display of integer warnings

on

Enable the display of integer warnings

query

Display the state of all integer warnings

s = intwarning('action') sets the state of integer warnings in MATLAB according to the string action, and then returns the previous state in a 4-by-1 structure array, s. The return structure array has two fields: identifier and state.

intwarning(s) sets the state of integer warnings in MATLAB according to the identifier and state fields in structure array s.

sOld = intwarning(sNew) sets the state of integer warnings in MATLAB according to sNew, and then returns the previous state in sOld.

Remarks

Examples

General Usage

Examples of the four types of integer warnings are shown here:

Attempt to convert NaN (Not a Number) to an unsigned integer:

uint8(NaN);
Warning: NaN converted to uint8(0).

Attempt to convert a floating point number to an unsigned integer:

uint8(2.7);
Warning: Conversion rounded non-integer floating point 
   value to nearest uint8 value.

Attempt to convert a large unsigned integer to a signed integer, where the operation overflows:

int8(uint8(200));
Warning: Out of range value converted to intmin('int8') 
   or intmax('int8').

Attempt an integer arithmetic operation that overflows:

intmax('uint8') + 5;
Warning: Out of range value or NaN computed in 
integer arithmetic.

Example 1

Check the initial state of integer warnings:

intwarning('query')
The state of warning 'MATLAB:intConvertNaN' is 'off'.
The state of warning 'MATLAB:intConvertNonIntVal' is 'off'.
The state of warning 'MATLAB:intConvertOverflow' is 'off'.
The state of warning 'MATLAB:intMathOverflow' is 'off'.

Convert a floating point value to an 8-bit unsigned integer. MATLAB does the conversion, but that requires rounding the resulting value. Because all integer warnings have been disabled, no warning is displayed:

uint8(2.7)
ans =
    3

Store this state in structure array iwState:

iwState = intwarning('query');

Change the state of the ConvertNonIntVal warning to 'on' by first setting the state to 'on' in the iwState structure array, and then loading iwState back into the internal integer warning settings for your MATLAB session:

maxintwarn = 4;

for k = 1:maxintwarn
   if strcmp(iwState(k).identifier, ...
             'MATLAB:intConvertNonIntVal')
      iwState(k).state = 'on';
      intwarning(iwState);
   end
end

Verify that the state of ConvertNonIntVal has changed:

intwarning('query')
The state of warning 'MATLAB:intConvertNaN' is 'off'.
The state of warning 'MATLAB:intConvertNonIntVal' is 'on'.
The state of warning 'MATLAB:intConvertOverflow' is 'off'.
The state of warning 'MATLAB:intMathOverflow' is 'off'.

Now repeat the conversion from floating point to integer. This time MATLAB displays the warning:

uint8(2.7)
Warning: Conversion rounded non-integer floating point
   value to nearest uint8 value.
ans =
    3

See Also

warning, lastwarn

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS