Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
imagesc NaN values

Subject: imagesc NaN values

From: Wendy

Date: 4 Apr, 2010 01:03:04

Message: 1 of 16

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

Subject: imagesc NaN values

From: ImageAnalyst

Date: 4 Apr, 2010 01:25:53

Message: 2 of 16

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.

Subject: imagesc NaN values

From: Wendy

Date: 4 Apr, 2010 01:44:05

Message: 3 of 16

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 <imageanalyst@mailinator.com> wrote in message <9d837299-3209-4da3-84ef-117ccf1919d4@y17g2000yqd.googlegroups.com>...
> 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.

Subject: imagesc NaN values

From: Wendy

Date: 4 Apr, 2010 01:47:05

Message: 4 of 16

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 <imageanalyst@mailinator.com> wrote in message <9d837299-3209-4da3-84ef-117ccf1919d4@y17g2000yqd.googlegroups.com>...
> 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.

Subject: imagesc NaN values

From: ImageAnalyst

Date: 4 Apr, 2010 02:21:34

Message: 5 of 16

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.

Subject: imagesc NaN values

From: Camille Couzi

Date: 17 Aug, 2010 14:34:04

Message: 6 of 16

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <7e7b84cb-f2b4-4a35-86ff-234e3855fdf3@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.

Subject: imagesc NaN values

From: Oliver Woodford

Date: 17 Aug, 2010 14:52:04

Message: 7 of 16

"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

Subject: imagesc NaN values

From: Camille Couzi

Date: 17 Aug, 2010 16:12:04

Message: 8 of 16

"Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4e7mk$qjc$1@fred.mathworks.com>...
> "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


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.

Subject: imagesc NaN values

From: Oliver Woodford

Date: 17 Aug, 2010 16:22:05

Message: 9 of 16

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4ecck$79l$1@fred.mathworks.com>...
> "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4e7mk$qjc$1@fred.mathworks.com>...
> > "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
>
>
> 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.

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

Subject: imagesc NaN values

From: Camille Couzi

Date: 17 Aug, 2010 16:25:19

Message: 10 of 16

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4ecck$79l$1@fred.mathworks.com>...
> "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4e7mk$qjc$1@fred.mathworks.com>...
> > "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
>
>
> 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.


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!!

Subject: imagesc NaN values

From: Camille Couzi

Date: 17 Aug, 2010 16:33:05

Message: 11 of 16

"Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4ecvd$eli$1@fred.mathworks.com>...
> "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4ecck$79l$1@fred.mathworks.com>...
> > "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4e7mk$qjc$1@fred.mathworks.com>...
> > > "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
> >
> >
> > 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.
>
> 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')


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!

Subject: imagesc NaN values

From: Oliver Woodford

Date: 18 Aug, 2010 09:00:26

Message: 12 of 16

"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

Subject: imagesc NaN values

From: Camille Couzi

Date: 18 Aug, 2010 14:25:24

Message: 13 of 16

"Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4g7fa$cd2$1@fred.mathworks.com>...
> "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

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!

Subject: imagesc NaN values

From: Camille Couzi

Date: 18 Aug, 2010 16:31:25

Message: 14 of 16

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4gqgk$ent$1@fred.mathworks.com>...
> "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4g7fa$cd2$1@fred.mathworks.com>...
> > "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
>
> 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!



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!!!

Subject: imagesc NaN values

From: Oliver Woodford

Date: 18 Aug, 2010 17:44:27

Message: 15 of 16

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4h1st$kj$1@fred.mathworks.com>...
> "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4gqgk$ent$1@fred.mathworks.com>...
> > "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4g7fa$cd2$1@fred.mathworks.com>...
> > > "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
> >
> > 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!
>
>
>
> 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!!!

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

Subject: imagesc NaN values

From: ClaudiaParise

Date: 28 Apr, 2014 13:43:09

Message: 16 of 16

"Oliver Woodford" wrote in message <i4ecvd$eli$1@fred.mathworks.com>...
> "Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <i4ecck$79l$1@fred.mathworks.com>...
> > "Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <i4e7mk$qjc$1@fred.mathworks.com>...
> > > "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
> >
> >
> > 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.
>
> 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')

=================================================

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 (clauoceano@gmail.com) the file?

Thanks in advance!
Claudia

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us