nantowhite

버전 1.0.0.0 (2.86 KB) 작성자: Benjamin Kraus
Convert a 2D matrix into an image with NaNs replaced by white
다운로드 수: 370
업데이트 날짜: 2013/5/28

라이선스 보기

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);

인용 양식

Benjamin Kraus (2025). nantowhite (https://www.mathworks.com/matlabcentral/fileexchange/41966-nantowhite), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2011b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Image Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0.0