Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imagesc, pcolor, and NaN

1,226 views
Skip to first unread message

Yves Gaudemer

unread,
Feb 1, 2007, 8:30:36 AM2/1/07
to
Hi,

Is there a way to display values of a matrix (size = [m,n]) with exactly
m rows and n columns (as with imagesc) AND Nan as empty patches (as with
pcolor) ?

Thanks in advance, Yves

Joe Lotz

unread,
Feb 1, 2007, 5:31:24 PM2/1/07
to
What I think you are asking... is the following:

old = magic(10)
new = NaN(10)
new(4:6,4:6) = old(4:6,4:6)

"old" is a 10x10 matrix, if I want to see the row 4-6 and columns
4-6, I can go old(4:6,4:6)
You can insert that "windowed" 3x3 matrix into a matrix of 10x10
NaN's; i.e NaN(10).

Not sure if that was what you are after or not.
cheers---joe

pipo

unread,
Feb 2, 2007, 4:57:09 AM2/2/07
to

Hi Yves,

a couple of weeks ago I was trying to accomplish the same thing as you
want to do. It would certainly be a nice feature of the imagesc function.

I used the following workaround that in my case gave very nice results:

1. replace the NaN's in your data with a value slightly larger than the
largest value of your data

maxval = max(data(:));
data(isnan(data)) = maxval + maxval/10;

2. Plot this data as you usually do

imagesc(data);

3. Now comes the trick: customize your colormap such that the largest
value of your data is painted white

% for making NaN values white
colordata = colormap;
colordata(end,:) = [1 1 1];
colormap(colordata);


I hope this works for you too.

Greetz,
P.

Yves Gaudemer

unread,
Feb 2, 2007, 10:25:55 AM2/2/07
to
Hi Joe and Pipo,

I have tried your suggestions : thanks ! I found also that adding an
extra row and an extra column could "fool" pcolor (used with shading
flat option) into displaying exactly the number of pixels of the
original data.

Cheers, Yves

Sergey

unread,
Jan 8, 2010, 6:48:02 PM1/8/10
to
try
h=imagesc(X,Y,C)
set(h,'alphadata',~isnan(C))

Yves Gaudemer <gaud...@ipgp.jussieu.fr> wrote in message <gaudemer-E4DC49...@vishnu.jussieu.fr>...

Bart Ribbens

unread,
Feb 10, 2010, 6:21:04 AM2/10/10
to
This solution works perfect! Thanks a lot!

Bart Ribbens

unread,
Feb 10, 2010, 6:21:04 AM2/10/10
to
This solution works perfect! Thanks a lot!

> try
> h=imagesc(X,Y,C)
> set(h,'alphadata',~isnan(C))

Jan Jensen

unread,
Oct 25, 2011, 6:47:13 AM10/25/11
to
I have exactly the same problem and this solution worked wonders for me.


The other solution has a bunch of challenges built into it: You must replace NaN with a value that is just the right amount above (below) the maximum (minimum) of your data. Do it wrong and the data maximum (minimum) and the NaN's will be displayed with the same color.

I found this thread through google search, and I'm giving it a bump to increase the likelyhood that others will find it :)

"Sergey " <fake...@gmail.net> wrote in message <hi8g7i$5v2$1...@fred.mathworks.com>...

JennoDitto

unread,
Dec 6, 2011, 6:17:09 AM12/6/11
to
Hi,
works quite fine in my case, too. Coloring mxn matrix filled with NaN's as "backcolor" with a color outside of the colorbar (in this case white) so that there is a clear difference.
Thanks.


"Jan Jensen" wrote in message <j8643h$pdm$1...@newscl01ah.mathworks.com>...

silvio Pereira

unread,
Oct 5, 2012, 7:22:09 AM10/5/12
to
Sergey's solution is brilliant!

"Sergey " <fake...@gmail.net> wrote in message <hi8g7i$5v2$1...@fred.mathworks.com>...

Anael

unread,
Mar 21, 2015, 12:42:55 AM3/21/15
to
This is exactly what I needed!
Thank you Sergey!

Chad Greene

unread,
Jan 25, 2017, 6:05:10 PM1/25/17
to
I've turned this solution into a simple function which is on File Exchange here: www.mathworks.com/matlabcentral/fileexchange/61293

Zbynek Vlasic

unread,
May 30, 2017, 1:34:16 PM5/30/17
to
Very simply solution. It works great, but my problem is that I need to use alpha 0.75 on visible data and alpha 0 for NaN. This solution do only unvisible NaN and visible others data. Is there another solution how to do it?

"Sergey" wrote in message <hi8g7i$5v2$1...@fred.mathworks.com>...

Brady Anderson

unread,
Jun 28, 2017, 9:30:20 AM6/28/17
to
Add a 0.75 multiplier to the output of the ~isnan() logic, inside the parentheses.

More specifically:

h=imagesc(X,Y,C)
set(h,'alphadata',~isnan(C)*0.75)

Does that do what you're looking for?



"Zbynek Vlasic" wrote in message <ogkaeb$7n4$1...@newscl01ah.mathworks.com>...
0 new messages