File Exchange

image thumbnail

imagescnan.m v2.1 (Aug 2009)

version (5.76 KB) by Carlos Adrian Vargas Aguilera
Same as IMAGESC but ignores NaNs.


Updated 20 Aug 2009

View Version History

View License

>> imagescnan(Z) % Uncolored NaNs
>> imagescnan(Z,'k') % Colored

IMAGESC is a good option when the user doesn't like the interpolating behaviour of SURF or PCOLOR, because EVERY element is drawn as a single pixel CENTERED with the axis value.

The problem with this function is that it doesn't work properly with NaNs elements (although sometimes SURF neither does): they are drawn with the lower color of the current colormap.

This functions uses IMAGESC but ignores the NaNs elements.

The user can specify the NaNs color, a different color mask, or another value to be ignored.

If the user gives X and Y data with no constant increment, PATCH is used instead of IMAGESC (this is experimental!).

To freeze the colormap check COLORFREEZE and CBFREEZE here at the FileExchange.

(Bugs reports and suggestions will be very appreciated)

Cite As

Carlos Adrian Vargas Aguilera (2021). imagescnan.m v2.1 (Aug 2009) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (28)

Andrew Diamond

It would be nice if you could get datatips to work fully. It shows the x,y position but not the value of the pixel it's over.

Paul Dysart

Brandon Kerns

Extremely useful for making color shaded plots for publication. PCOLOR gets rendered as patches in a pdf file, which is not optimal because you see the edges of the patches. Using imagesc or isagescnan outputs the shading as an embedded image. There is one bug in which I had the X and Y coordinates as a regular grid with 0.1 intervals, and it thought the grid was irregular and reverted to patches. Fixed by changing 1000 to 10000 on lines 231 and 284. This seemed to work with 0.01 intervals too.


ehhh... ok, I fell for the same xy-switch bug. Why would one switch it?

I wrote my custom version now which switches this automatically, far too confusing otherwise :)


nice function in general, but I run into errors (dimension mismatch in line 226) quite often when the matlab-native imagesc gives no errors.
I then just go and use it without x,y vectors but that's not very satisfactory.

Noah Levine-Small

The mirrored axis problem is a general matlab bug. Try setting opengl to software.


Nicolas Beuchat

Excellent function, I had a little function that put the NaN white but this one is much more flexible! Thanks!

Mario Trevino


Felipe G. Nievinski

why not do simply:
imagesc(img, 'AlphaData',~isnan(img))
where, e.g.,
img = membrane(1,25);
img(1:10:end) = NaN;


Hi, thanks a lot this was very helpful. However, as Camille has pointed out, the axis lebels appear mirrored at times. This doesnt happen all the time, but mostly at times when there are too many NaNs in the Matrix. Is there a workaround?

Camille Couzi

Hi again, I am having the same problem as Yu-nung Nina in 2009:
"Axis labels of both axes flip (the numbers become "mirror image" of normal axis tick labels) "
Please can you write here what solution you found to that problem?
Thanks a lot

Camille Couzi

Millones de gracias por esta herramienta super util y facil de usar!!!!


Yes Olivier, or with:
>> imagesc(U,'AlphaData',~isnan(U),'AlphaDataMapping','none')
I just wanted to be able to paint NaNs with specifyc color without modifying the axes color.
Probably I should include this "transparent" option. Thank you!


Olivier Ripoll

another way to obtain this is simply (fo a given array M):


You can of course get all the imagesc options since you can it directly.


David Wang

Anyway, that won't make life easier. Probably I should simply do this:

pcolor((x(1:end-1)+x(2:end))/2, (y(1:end-1)+y(2:end))/2, c(2:end,2:end));

It's not perfect. The first/last column/row get dropped. But it should be sufficient for my use...

David Wang

Thanks for your response, Saludos.

First, PCOLOR(X,Y,C) regards X,Y as the edges of pixels, while IMAGESC(X,Y,C) regards X,Y as the centers of pixels. The latter is what I need.

Second, what I want is not 'interp' but 'faceted', namely facet lines at the edges of pixels, which is what PCOLOR does by default:


Yes (kind of) but first, why would you like to do so if you can do it with PCOLOR? Well...

% Change line 359:
if aequal
if 0

% On empty line 398 write:
U = [U U(:,end); U(end,:) U(end,end)];
% Which is the trick. You may try other ways.

% Finally, change line 404:
% by:

% And that is it. Try imagescnan(peaks(5)) and you'll see the problem, which is that the interpolation is done on the edges and not on the middle of the faces (sort of). Maybe some day...

Saludos, Carlos Vargas

David Wang

This is a great script. I've got a relevant question. Do you know if it is possible to make imagesc/imagescnan draw facet lines as shading facets applies to pcolor?


I guess not Yu-nung, the program may flip the image if your axes are decreasing, but never the ticks. Try a normal IMAGESC or send me your script. Carlos

Yu-nung Nina

One small mistake in my comment: on my personal laptop, the plot looks normal WITHOUT these two bizard things happening.

Yu-nung Nina

Hi, this script is fantastic. However, I found 2 strange things happen:

1. Axis labels of both axes flip (the numbers become "mirror image" of normal axis tick labels)
2. When I try to plot a matrix of size 420x810, the plot does not complete itself. Instead the script only plots the first few rows and stops.

These two things happen on my lab computer (with matlab R2006a, R2008a, R2008b on it; I tried all of them). But on my personal laptop, the plot looks normal with these two bizard things happening. Is there any special settings in your code that is related to the computer itself?

Gus Silva

This problem has been bothering me for a very long time. Thanks allot!!!

Carlos Adrian Vargas Aguilera

Thank you very much Greg!, I already sent and updated file, which also accepts RGB images.

Cheers, Carlos

Greg King

imagescnan did just what I wanted (i.e., color NaNs white). However, exploring the color option I found a bug. The doc says that "color" can be a 3 element vector or a character ('k', 'w', etc). In fact only the latter option works. The 3 element color vector results in an error.

Carlos Adrian Vargas Aguilera

I submited an updated file that freezes correctly the colorbar.

Carlos Vargas

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

Community Treasure Hunt

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

Start Hunting!