Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>NaN values get the first color from the axes colormap, which by default corresponds to the minimum value (other than NaN). You can change the color for minimum value setting axes color limits with <a href="http://www.mathworks.com/help/techdoc/ref/caxis.html" rel="noreferrer">CAXIS</a> function. To assign a contrast color to NaN values you can add a special color for NaN values as a first color (1x3 vector).</p> <p>I take your example and made a function (with some comments):</p> <pre><code>function [h hcb] = imagescwithnan(a,cm,nanclr) % IMAGESC with NaNs assigning a specific color to NaNs %# find minimum and maximum amin=min(a(:)); amax=max(a(:)); %# size of colormap n = size(cm,1); %# color step dmap=(amax-amin)/n; %# standard imagesc him = imagesc(a); %# add nan color to colormap colormap([nanclr; cm]); %# changing color limits caxis([amin-dmap amax]); %# place a colorbar hcb = colorbar; %# change Y limit for colorbar to avoid showing NaN color ylim(hcb,[amin amax]) if nargout &gt; 0 h = him; end </code></pre> <p>Here caxis statement assigns the first color of the color map not to the minimum value <code>amin</code>, but to the <code>amin-dmap</code>. So the first color get assigned specifically to NaNs.</p> <hr> <p>Try this function with:</p> <pre><code>a=peaks; a(a &lt; 0.5) = nan; imagescwithnan(a,hot,[0 1 1]) %# [0 1 1] is cyan </code></pre> <p><img src="https://i.stack.imgur.com/9VsPu.png" alt="test image - NaN color is hidden"></p> <p>If you comment the ylim statement in the function (can be control with additional parameter) this NaN color will be on the colormap.</p> <p><img src="https://i.stack.imgur.com/jJ3o6.png" alt="test image - NaN color is shown on the colorbar"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload