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

imagesc NaN values

386 views
Skip to first unread message

Wendy

unread,
Apr 3, 2010, 9:03:04 PM4/3/10
to
Hi all,

I read in a matrix from xls file. The matrix is as follows

pvalue = [ NaN NaN NaN NaN;
NaN NaN 1.00000000000000e-05 NaN;
NaN 4.00000000000000e-05 0 NaN;
NaN 0 0 NaN;
NaN NaN NaN NaN];

All the NaN are empty elements in the excel file. I want to plot the pvalue using imagesc and indicate the empty elements as the background colour. However, Matlab treats the NaN as the minimum value (0) and indicates those 'NaN' positions blue on the plot. Is there a way that I can plot the 'NaN' elements as the background colour and only plot those elements that I have value with?

Hope I made the question clear. Thank you very much,
Wendy

ImageAnalyst

unread,
Apr 3, 2010, 9:25:53 PM4/3/10
to
Wendy:
In order to assign it a color, it has to have a value. You can assign
the nan's a value like this:
pvalue(isnan(pvalue)) = desiredValue;
If desiredValue already exists in your pvalue array, then you those
will be indistinguishable from the nans that were newly set to the
same values. You can maybe set it up to use some unused value - take
the histogram and check for gaps. If, say, you're using uint8 values
and every value from 0 to 255 is being used by some pixel, then I
think you'd need to reuse a value.

Anyway, once you've set the nan's to the new value, then you need to
set up a colormap where the color at the index of the new value is the
color that you desire the nan pixels to have.

Wendy

unread,
Apr 3, 2010, 9:44:05 PM4/3/10
to
Thank you very much, ImageAnalyst. As all my pvalues are >=0, I assigned a negative value to NaN elements (-2), and I set caxis to [0 max(max(pvalues))]. The -2 elements are still treated as minimum number. Do you mind to give some hint on how to set a value to the desiredValue, for example, set -2 elements to grey. What functions shall I look at in matlab?

Thank you,
Wendy


ImageAnalyst <imagea...@mailinator.com> wrote in message <9d837299-3209-4da3...@y17g2000yqd.googlegroups.com>...

Wendy

unread,
Apr 3, 2010, 9:47:05 PM4/3/10
to
Thank you very much. As all my pvalues are >=0, I assigned -2 to NaN elements, then I set caxis([0 max(max(pvalue))]. However, -2 elements are still treated as minimum values as they outside the caxis. Do you mind to give me some hint on how to set -2 elements to grey color? What functions shall I look at in matlab?

Thank you very much.
Wendy

ImageAnalyst

unread,
Apr 3, 2010, 10:21:34 PM4/3/10
to
I don't know - you'd have to experiment. I rarely use imagesc,
favoring imshow instead. And when I apply a pseudocolor lookup table
(colormap) I'm always dealing with non-negative pixel values.

Camille Couzi

unread,
Aug 17, 2010, 10:34:04 AM8/17/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <7e7b84cb-f2b4-4a35...@q16g2000yqq.googlegroups.com>...

> I don't know - you'd have to experiment. I rarely use imagesc,
> favoring imshow instead. And when I apply a pseudocolor lookup table
> (colormap) I'm always dealing with non-negative pixel values.

Hi Wendy,

I am having the same problem as you, I have a matrix with NaN values and I am displaying them on an imagesc.
Did you get a solution for the problem? how do you display (for example) NaN values in grey?
Thanks in advance for your information.

Oliver Woodford

unread,
Aug 17, 2010, 10:52:04 AM8/17/10
to
"Camille Couzi" wrote:
I have a matrix with NaN values and I am displaying them on an imagesc.

% Data:
data = peaks(256);
data(rand(size(data))<0.01) = NaN;

% Command:
sc(data, 'jet', [0.5 0.5 0.5]);

You can download sc here:
http://www.mathworks.com/matlabcentral/fileexchange/16233-sc-powerful-image-rendering

HTH,
Oliver

Camille Couzi

unread,
Aug 17, 2010, 12:12:04 PM8/17/10
to
"Oliver Woodford" <o.j.woo...@cantab.net> wrote in message <i4e7mk$qjc$1...@fred.mathworks.com>...


Hi Oliver!!!
Thank you very much for your reply!
I have tested sc with your example, and it worked ok, and now I am trying it on my matrix (which is (3732*12) and it appears like a (1*12) on the figure...
What could be the problem?
Thank you so much for your help :-)

Camille.

Oliver Woodford

unread,
Aug 17, 2010, 12:22:05 PM8/17/10
to
"Camille Couzi" <camill...@yahoo.fr> wrote in message <i4ecck$79l$1...@fred.mathworks.com>...

I have no idea what the problem is. This works fine for me:
>> A = rand(3732, 12);
>> A(rand(size(A))<0.01) = NaN;
>> sc(A, 'jet', 'k')

Camille Couzi

unread,
Aug 17, 2010, 12:25:19 PM8/17/10
to
"Camille Couzi" <camill...@yahoo.fr> wrote in message <i4ecck$79l$1...@fred.mathworks.com>...


Hi again,
I found myself the answer to my question reading all the posts in the sc page:

"Justin - if you prefer the image displaying properties of imagesc I suggest you use image(sc(X)). However, you then don't get the correct colorbar. If it's just the aspect ratio you don't like then call set(gca, 'DataAspectRatioMode', 'auto') after sc."

I add set(gca, 'DataAspectRatioMode', 'auto') after my sc command and that worked.

THANK YOU A LOT OLIVER!!

Camille Couzi

unread,
Aug 17, 2010, 12:33:05 PM8/17/10
to
"Oliver Woodford" <o.j.woo...@cantab.net> wrote in message <i4ecvd$eli$1...@fred.mathworks.com>...


Hi oliver,
Thanks again, I have found the solution to this problem.
1 more question: I have seted the naN value to black. this color appear in the colorbar, just on the bottom of the colorbar. is there a way not to see this black color, so that my colorbar begins at the real minimum value (not NaN)
Again, thank you a lot for your very usefull tool, I have been searching that type of thing a long time and had many problems with other tools...
Thank you!

Oliver Woodford

unread,
Aug 18, 2010, 5:00:26 AM8/18/10
to
"Camille Couzi" wrote:
> 1 more question: I have seted the naN value to black. this color appear in the colorbar, just on the bottom of the colorbar. is there a way not to see this black color, so that my colorbar begins at the real minimum value (not NaN)

Camille,

When I try:

data = peaks(256);
data(rand(size(data))<0.01) = NaN;

sc(data, 'jet', [0 0 0]);
colorbar

I do not see the color black at the bottom of the colorbar. If you change the NaN color to red you will not see red either.

I updated sc yesterday. Make sure you are using the latest version so we are using the same code.

Regards,
Oliver

Camille Couzi

unread,
Aug 18, 2010, 10:25:24 AM8/18/10
to
"Oliver Woodford" <o.j.woo...@cantab.net> wrote in message <i4g7fa$cd2$1...@fred.mathworks.com>...

Hi Oliver,
In fact I had downloaded apparently a old version of sc, I just went to your page and downloaded the newest version, which is a complete folder (the one I downloaded yesterday was a single sc.m file).
I will try with this and tell you the results when I get them.
Thanks again!

Camille Couzi

unread,
Aug 18, 2010, 12:31:25 PM8/18/10
to
"Camille Couzi" <camill...@yahoo.fr> wrote in message <i4gqgk$ent$1...@fred.mathworks.com>...

Hi Oliver,
Well, I have tried it with all the suggestions you gave me and that work great! xis on, labels, no black color on the colorbar...
Nevertheless, I have a little problem. I have a buttondownfcn defined in my GUI, which displays a vertical line where you click and plot the corresponding profile (values of z) on another axe. When I have my subplot made by imagesc that work perfectly (but I don't have all the advantages of the sc, of course), but when I use sc, and I click on my subplot, then nothing happens (like if the buttondownfc where disabled by sc), and on the toolbar I cn see that the colorbar button is pushed, active.
can you help me fixing that...
thank you so much for all your help, your function is really really fantastic though!!!

Oliver Woodford

unread,
Aug 18, 2010, 1:44:27 PM8/18/10
to
"Camille Couzi" <camill...@yahoo.fr> wrote in message <i4h1st$kj$1...@fred.mathworks.com>...

Hi Camille

I'm surprised to hear about the button callback not working. If you email me directly the minimal amount of code required to replicate the problem, then I'll take a look.

Thanks,
Oliver

ClaudiaParise

unread,
Apr 28, 2014, 9:43:09 AM4/28/14
to
"Oliver Woodford" wrote in message <i4ecvd$eli$1...@fred.mathworks.com>...
=================================================

Hi Oliver, Camille,

I have the same problem as Camille had and I would like to get the Oliver's matlab function (sc). I have tried using the Oliver's link above but I could not see the function to donwload.
Please, could any of you kindly email me (clauo...@gmail.com) the file?

Thanks in advance!
Claudia

Elly Wang

unread,
Sep 3, 2015, 3:18:09 PM9/3/15
to
"Oliver Woodford" wrote in message <i4e7mk$qjc$1...@fred.mathworks.com>...
Thank you so much for providing this. This is exactly what I need.

Simone Sabbatini

unread,
Aug 22, 2017, 10:51:17 AM8/22/17
to
"Elly Wang" wrote in message <msa6db$51k$1...@newscl01ah.mathworks.com>...

Simone Sabbatini

unread,
Aug 22, 2017, 11:14:18 AM8/22/17
to
"Elly Wang" wrote in message <msa6db$51k$1...@newscl01ah.mathworks.com>...
Hi Oliver,
I report this behaviour of your function sc:
I am in a similar situation of that of the posts above, then I used the sc function as to represent in grey the NaN values of a matrix data (n X 3). I got some troubles as adding the yticklabels did not work:
set(gca,'yticklabel',labels)
I then followed the suggestion of using image(sc(data,'jet',[0.5 0.5 0.5])) reported in a previous post, and it worked but the colorbar scale was messed up. Then I tried imagesc(sc(data)) and everything worked fine...

Thanks for your help

Simone
0 new messages