## nantowhite

version 1.0.0.0 (2.86 KB) by
Convert a 2D matrix into an image with NaNs replaced by white

Updated 28 May 2013

nantowhite - Convert a 2D matrix into an image with NaNs replaced by white

USAGE:
[im, clim] = nantowhite(cvals)
[im, clim] = nantowhite(cvals, clim)
[im, clim] = nantowhite(cvals, clim, cmap)

nantowhite takes in an N x M matrix of values ('cvals') and outputs an N x M x 3 uint8 image that can be displayed on screen using 'image'. This function replaces 'imagesc' for situations where there are NaN values in the input matrix. Instead of displaying 'NaN' values using the first value in the colormap, it displays them using white. You can replace what would originally be 'imagesc(cvals)' with 'image(nantowhite(cvals))'.

This function was created because alpha layers used to make 'NaN' values transparent were ignored when images were saved in a vector format.

See the demo code below to get 'colorbar' to show the correct limits when using this function.

INPUT:
cvals - A 2D matrix to be converted to an image. Any NaN values in the
matrix will be converted into white pixels in the output image.

clim - The minimum and maximum value on the colorscale used for
converting values in 'cvals' into colors in the output image.
An empty maxtix is ignored. Positive or negative 'inf' values
are replaced by the minimum or maximum value in 'cvals'.

cmap - The color map to use for conversion from 'cvals' into colors.

OUTPUT:
im - An N x M x 3 uint8 image matrix. [N,M] = size(cvals)

clim - The color limits used when converting values to color pixels.

DEMO:

[X,Y] = meshgrid(-2*pi:pi/16:2*pi);
cvals = 20*cos(X).*sin(Y);
cvals(1:20,1:20) = NaN;
cvals(randperm(numel(cvals),400)) = NaN;

Original Code:
figure; ax = axes();
imagesc(cvals,'Parent',ax);
colorbar('Peer',ax);

Modified Code:
figure; ax = axes();
[im, cl] = nantowhite(cvals);
image(im,'Parent',ax,'CDataMapping','scaled');
set(ax,'CLim',cl);
colorbar('Peer',ax);

### Cite As

Benjamin Kraus (2022). nantowhite (https://www.mathworks.com/matlabcentral/fileexchange/41966-nantowhite), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2011b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux