


IMAGESCNAN Scale data and display as image with uncolored NaNs.
Syntax
imagescnan(x,y,U)
imagescnan(x,y,U,...,colormask)
imagescnan(x,y,U,...,color)
imagescnan(x,y,U,...,cbfit_opt)
h = imagescnan(...);
[h,hcb] = imagescnan(...,cbfit_opt);
Input:
x - X-axis vector data. Optional, i.e., can be empty.
Default: 1:n (rows index).
y - Y-axis vector data. Optional, i.e., can be empty.
Default: 1:m (column index).
U - Matrix [m x n] data or an RGB image [m x n x 3] (with/
without NaNs).
colormask - Logical matrix indicating the U elements to be
uncolored, if is empty then ISNAN(U) is used. Or it can
be a single value which will be uncolored.
Default: ~isfinite(U) (NaNs and Infs elements uncolored)
color - A vector with 3 elements specifying the [R G B] color
for the NaNs color. It can be specified by the known
char colors: 'k', etcerera. Optional.
Default: get(gca,'color') (axes background color)
cbfit_opt - Cell array with the options to call COLORBARFIT.
Default: COLORBARFIT function is not used by default.
Output:
h - Image handle. Optional
hcb - Colorbar handle. Optional
Description:
This function draws a matrix data as an image with uncolored NaN's
elements using IMAGESC. The difference between IMAGESC and the
PCOLOR, MESH or SURF function is that EVERY element is colored and
no one is interpolated, besides, the pixels are centered with the
axis value, although it is a flat image.
The color mask is added because IMAGESC do not work with NaN's, in
fact it colors them with the lower value of the current colormap.
The cbfit_opt is include in order to be able to define a diferent
color map with the COLORBARFIT function which can be found at:
http://www.mathworks.com/matlabcentral/fileexchange/.
If this function is not found, a normal COLORBAR is generated.
The data and the colorbar are scaled with the current colormap, so,
the use of COLORMAP after this function doesn't affects the
generated image and colorbar! Therefore, COLORMAP and CAXIS should
be used before this function.
Notes: * The inputs arguments for the COLORBARFIT function are 3
plus the normal COLORBAR function options, for this reason,
if the former is not found, the latter is used ignoring
these first 3 options. Anyway, to generate a colorbar, at
least an empty cell is needed for cbfit_opt = {[]}.
Examples:
% Compares with normal IMAGESC:
N = 100;
PNaNs = 0.10;
X = peaks(N);
X(round(1 + (N^2-1).*rand(N^2*PNaNs,1))) = NaN;
subplot(221), imagesc(X)
title('With IMAGESC: ugly NaNs')
subplot(222), imagescnan([],[],X)
title('With IMAGESCNAN: uncolored NaNs')
% Compares with SPY:
subplot(223), spy(isnan(X))
title('SPY NaNs')
subplot(224), imagescnan([],[],isnan(X),0), axis equal tight
title('No-NaNs with IMAGESCNAN')
See also IMAGE, IMAGESC, COLORBAR, IMREAD, IMWRITE and COLORBARFIT by
Carlos Vargas.

0001 function [h,hcb] = imagescnan(x,y,U,varargin) 0002 %IMAGESCNAN Scale data and display as image with uncolored NaNs. 0003 % 0004 % Syntax 0005 % imagescnan(x,y,U) 0006 % imagescnan(x,y,U,...,colormask) 0007 % imagescnan(x,y,U,...,color) 0008 % imagescnan(x,y,U,...,cbfit_opt) 0009 % h = imagescnan(...); 0010 % [h,hcb] = imagescnan(...,cbfit_opt); 0011 % 0012 % Input: 0013 % x - X-axis vector data. Optional, i.e., can be empty. 0014 % Default: 1:n (rows index). 0015 % y - Y-axis vector data. Optional, i.e., can be empty. 0016 % Default: 1:m (column index). 0017 % U - Matrix [m x n] data or an RGB image [m x n x 3] (with/ 0018 % without NaNs). 0019 % colormask - Logical matrix indicating the U elements to be 0020 % uncolored, if is empty then ISNAN(U) is used. Or it can 0021 % be a single value which will be uncolored. 0022 % Default: ~isfinite(U) (NaNs and Infs elements uncolored) 0023 % color - A vector with 3 elements specifying the [R G B] color 0024 % for the NaNs color. It can be specified by the known 0025 % char colors: 'k', etcerera. Optional. 0026 % Default: get(gca,'color') (axes background color) 0027 % cbfit_opt - Cell array with the options to call COLORBARFIT. 0028 % Default: COLORBARFIT function is not used by default. 0029 % 0030 % Output: 0031 % h - Image handle. Optional 0032 % hcb - Colorbar handle. Optional 0033 % 0034 % Description: 0035 % This function draws a matrix data as an image with uncolored NaN's 0036 % elements using IMAGESC. The difference between IMAGESC and the 0037 % PCOLOR, MESH or SURF function is that EVERY element is colored and 0038 % no one is interpolated, besides, the pixels are centered with the 0039 % axis value, although it is a flat image. 0040 % 0041 % The color mask is added because IMAGESC do not work with NaN's, in 0042 % fact it colors them with the lower value of the current colormap. 0043 % 0044 % The cbfit_opt is include in order to be able to define a diferent 0045 % color map with the COLORBARFIT function which can be found at: 0046 % http://www.mathworks.com/matlabcentral/fileexchange/. 0047 % If this function is not found, a normal COLORBAR is generated. 0048 % 0049 % The data and the colorbar are scaled with the current colormap, so, 0050 % the use of COLORMAP after this function doesn't affects the 0051 % generated image and colorbar! Therefore, COLORMAP and CAXIS should 0052 % be used before this function. 0053 % 0054 % Notes: * The inputs arguments for the COLORBARFIT function are 3 0055 % plus the normal COLORBAR function options, for this reason, 0056 % if the former is not found, the latter is used ignoring 0057 % these first 3 options. Anyway, to generate a colorbar, at 0058 % least an empty cell is needed for cbfit_opt = {[]}. 0059 % 0060 % Examples: 0061 % 0062 % % Compares with normal IMAGESC: 0063 % N = 100; 0064 % PNaNs = 0.10; 0065 % X = peaks(N); 0066 % X(round(1 + (N^2-1).*rand(N^2*PNaNs,1))) = NaN; 0067 % subplot(221), imagesc(X) 0068 % title('With IMAGESC: ugly NaNs') 0069 % subplot(222), imagescnan([],[],X) 0070 % title('With IMAGESCNAN: uncolored NaNs') 0071 % 0072 % % Compares with SPY: 0073 % subplot(223), spy(isnan(X)) 0074 % title('SPY NaNs') 0075 % subplot(224), imagescnan([],[],isnan(X),0), axis equal tight 0076 % title('No-NaNs with IMAGESCNAN') 0077 % 0078 % See also IMAGE, IMAGESC, COLORBAR, IMREAD, IMWRITE and COLORBARFIT by 0079 % Carlos Vargas. 0080 0081 % Copyright 2008 Carlos Adrian Vargas Aguilera 0082 % $Revision: 1.1 $ $Date: 2009/03/24 21:05:20 $ 0083 0084 % Written by 0085 % M.S. Carlos Adrian Vargas Aguilera 0086 % Physical Oceanography PhD candidate 0087 % CICESE 0088 % Mexico, 2008 0089 % nubeobscura@hotmail.com 0090 % 0091 % Download from: 0092 % http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objec 0093 % tType=author&objectId=1093874 0094 0095 % 1.0 Released (30/06/2008) 0096 % 1.1 Fixed bug when CAXIS used. 0097 % 1.2 Colorbar freezed colormap. 0098 % 1.3 Fixed bug in color vector input (Found by Greg King) and now 0099 % accets RGB image as input. 0100 0101 %% INPUTS: 0102 0103 % Error checking: 0104 % Note: At least 3 inputs and no more than 6: 0105 if nargin<3 || nargin>6 0106 error('Imagescnan:IncorrectInputNumber',... 0107 'Input arguments must be at least 3 and less than 7.') 0108 end 0109 0110 % Check the x,y,U: 0111 % Note: x,y should be the axes data. 0112 m = size(U); 0113 if numel(m)>3 0114 error('Imagescnan:IncorrectInputSize',... 0115 'Input image must be a matrix or an RGB image.') 0116 else 0117 if isempty(x) || numel(x)~=m(2) 0118 %warning('Imagescnan:IncorrectInputSize',... 0119 % 'Index column axis has been used.') 0120 x = 1:m(2); 0121 end 0122 if isempty(y) || numel(y)~=m(1) 0123 %warning('Imagescnan:IncorrectInputSize',... 0124 % 'Index row axis has been used.') 0125 y = 1:m(1); 0126 end 0127 end 0128 0129 % Get color limits: 0130 % Note: If you would like to use specific color limits, use CAXIS before 0131 % this function. 0132 switch get(gca,'CLimMode') 0133 case 'manual' 0134 clim = caxis; 0135 otherwise 0136 clim = [min(U(:)) max(U(:))]; 0137 end 0138 0139 % Parse inputs and defaults: 0140 % Note: * Mask color will be the not-finite elements plus the elements 0141 % indicated by the user. 0142 % * Default colormask is the current axes background. 0143 % * Default currentmap is current figure colormap (probably JET). 0144 colormask = ~isfinite(U); 0145 color_nan = get(gca,'color'); 0146 color_map = get(gcf,'colormap'); 0147 cbfit_opt = []; 0148 ycolorbarfit = (exist('colorbarfit','file')==2); 0149 if nargin>3 0150 while ~isempty(varargin) 0151 if iscell(varargin{1}) 0152 if length(varargin{1})<3 0153 error('Imagescnan:IncorrectInputType',... 0154 'Options for COLORBARFIT must be at least 3, although empty.') 0155 end 0156 caxis(clim) 0157 cbfit_opt = varargin{1}; 0158 if ycolorbarfit 0159 colorbarfit(cbfit_opt{:}) 0160 color_map = get(gcf,'colormap'); 0161 else 0162 % warning('Imagescnan:ColorBarFitNotFound',... 0163 % 'COLORBARFIT function not found, used default COLORBAR.') 0164 end 0165 varargin(1) = []; 0166 elseif ischar(varargin{1}) 0167 switch varargin{1} 0168 case 'y', color_nan = [1 1 0]; 0169 case 'm', color_nan = [1 0 0]; 0170 case 'c', color_nan = [0 1 1]; 0171 case 'r', color_nan = [1 0 0]; 0172 case 'g', color_nan = [0 1 0]; 0173 case 'b', color_nan = [0 0 1]; 0174 case 'w', color_nan = [1 1 1]; 0175 case 'k', color_nan = [0 0 0]; 0176 otherwise 0177 error('Imagescnan:InvalidColor',... 0178 'Color char must be one of: ''ymcrgbwk''.') 0179 end 0180 varargin(1) = []; 0181 elseif islogical(varargin{1}) 0182 if numel(varargin{1})~=numel(U) 0183 error('Imagescnan:InvalidMask',... 0184 'The logical mask must have the same elements as the matrix.') 0185 end 0186 colormask = varargin{1} | colormask; 0187 varargin(1) = []; 0188 elseif length(varargin{1})==3 0189 if (max(varargin{1})>1) || (min(varargin{1})<0) % Fixed BUG 2008/07/11 0190 error('Imagescnan:InvalidColor',... 0191 'The color must be on the range of [0 1].') 0192 end 0193 color_nan = varargin{1}; 0194 varargin(1) = []; 0195 elseif length(varargin{1})==1 0196 colormask = (U==varargin{1}) | colormask; 0197 varargin(1) = []; 0198 else 0199 error('Imagescnan:IncorrectInputType',... 0200 'Incorrect optional(s) argument(s).') 0201 end 0202 end 0203 end 0204 0205 0206 %% MAIN: 0207 0208 % Matrix data to RGB: 0209 if numel(m)==2 0210 0211 % Sets to double data: 0212 if ~isfloat(U) 0213 U = double(U); 0214 end 0215 0216 % Normalizes and rounds data to range [0 N]: 0217 N = size(color_map,1); 0218 U = (U - clim(1))/diff(clim); % Fixed bug when CAXIS used 0219 U = U*N; 0220 if N<=256 0221 U = uint8(U); 0222 else 0223 U = uint16(U); 0224 end 0225 0226 % Scales data with colormap: 0227 U = ind2rgb(U,color_map); % 2D to 3D RGB values [0 1] 0228 else 0229 % Already is an RGB image, so do nothing. 0230 end 0231 0232 % Set mask color to color_nan: 0233 mn = prod(m(1:2)); 0234 ind = find(colormask); 0235 U(ind) = color_nan(1); % Red color 0236 U(ind+mn) = color_nan(2); % Green color 0237 U(ind+mn*2) = color_nan(3); % Blue color 0238 0239 % Draws the RGB image: 0240 h = imagesc(x,y,U,clim); 0241 0242 %% OUTPUTS: 0243 0244 % Calls to colorbarfit and freezes his colormap: 0245 if ~isempty(cbfit_opt) 0246 % Creates a temporary colorbar: 0247 if ycolorbarfit 0248 hcb = colorbarfit(cbfit_opt{:}); 0249 else 0250 Nopt = min([3 length(cbfit_opt)]); 0251 cbfit_opt(1:Nopt) = []; 0252 hcb = colorbar(cbfit_opt{:}); 0253 end 0254 % Save image position: 0255 ha = gca; position = get(ha,'Position'); 0256 % Gets colorbar axes properties: 0257 ghcb = get(hcb); 0258 CData = ind2rgb(get(ghcb.Children,'CData'),color_map); 0259 XData = get(ghcb.Children,'XData'); 0260 YData = get(ghcb.Children,'YData'); 0261 % Move ticks because IMAGESC draws them like centered pixels: 0262 XTick = ghcb.XTick; 0263 YTick = ghcb.YTick; 0264 if ~isempty(XTick) 0265 XTick = XTick(1:end-1) + diff(XTick(1:2))/2; 0266 end 0267 if ~isempty(YTick) 0268 YTick = YTick(1:end-1) + diff(YTick(1:2))/2; 0269 end 0270 % Deletes the colorbar: 0271 delete(hcb) 0272 % Generates other colorbar: 0273 hcb = axes('Position',ghcb.Position); 0274 hcbim = imagesc(XTick,YTick,CData,'Parent',hcb); axis tight 0275 set(hcbim,... 0276 'HitTest','off',... 0277 'Interruptible','off',... 0278 'SelectionHighlight','off',... 0279 'Tag','TMW_COLORBAR',... 0280 'XData',XData,... 0281 'YData',YData) 0282 set(hcb,... 0283 'XAxisLocation',ghcb.XAxisLocation,... 0284 'YAxisLocation',ghcb.YAxisLocation,... 0285 'XLim',ghcb.XLim,... 0286 'YLim',ghcb.YLim,... 0287 'XDir',ghcb.XDir,... 0288 'YDir',ghcb.YDir,... 0289 'XTick',ghcb.XTick,... 0290 'YTick',ghcb.YTick,... 0291 'XTickLabel',ghcb.XTickLabel,... 0292 'YTickLabel',ghcb.YTickLabel,... 0293 'ButtonDownFcn',@resetCurrentAxes,... 0294 'Interruptible','off',... 0295 'Tag','Colorbar') 0296 % Returns the image position: 0297 axes(ha), set(ha,'Position',position) 0298 end 0299 0300 % Sets output: 0301 if ~nargout 0302 clear h 0303 end