MATLAB Function Reference  intwarning

Control state of integer warnings

Syntax

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

Description

MATLAB has four types of integer warnings. The `intwarning` function enables, disables, or returns information on these warnings:

• `MATLAB:intConvertNaN` -- Warning on an attempt to convert NaN (Not a Number) to an integer. The result of the operation is zero.
• `MATLAB:intConvertNonIntVal` -- Warning on an attempt to convert a non-integer value to an integer. The result is that the input value is rounded to the nearest integer for that class.
• `MATLAB:intConvertOverflow` -- Warning on overflow when attempting to convert from a numeric class to an integer class. The result is the maximum value for the target class.
• `MATLAB:intMathOverflow` -- Warning on overflow when attempting an integer arithmetic operation. The result is the maximum value for the class of the input value. MATLAB also issues this warning when NaN is computed (e.g., `int8(0)/0`).

```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

 Caution    Enabling the `MATLAB:intMathOverflow` warning slows down integer arithmetic. It is recommended that you enable this particular warning only when you need to diagnose unusual behavior in your code, and disable it during normal program operation. The other integer warnings listed here do not affect program performance.

Examples

General Usage

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

• MATLAB:intConvertNaN

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

• ```uint8(NaN);
Warning: NaN converted to uint8(0).
```
• MATLAB:intConvertNonIntVal

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.
```
• MATLAB:intConvertOverflow

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').
```
• MATLAB:intMathOverflow

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`

© 1994-2005 The MathWorks, Inc.