MATLAB: Pcolor – how to remove NaN text / string

nanpcolorplottext string

Hi all,
I'm having a problem with pcolor plot where "I don't want to display NaN value" in my plot (figure attached). I only want to show real values. My code as follow and example of data is attached too. Maybe I should use other function instead of num2str? Any advise and suggestion would be very great! appreciate your time.
figure
H = pcolor(xedges,yedges,Ab);
colormap(cm);
set(H, 'linestyle', 'none');
pos=get(gca,'position'); % get plot position
[rows,cols]=size(Ab); % get cols and rows size
width=pos(3)/(cols-1); % get width and height positions
height =pos(4)/(rows-1);
for i=1:cols-1 % plot text in the figure
for j=rows-1:-1:1 annotation('textbox',[pos(1)+width*(i-1),pos(2)+height*(j-1),width,height], ...
'string',num2str(Ab(j,i)),'LineStyle','none','HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
end
set(gca,'Xlim',[0 30]);
set(gca,'XTick',[0:3:30]);
set(gca,'Ylim',[0 5]);
set(gca,'YTick',[0:1:5]);
set(gca,'FontSize',10,'FontWeight','Bold');
the code above is originally from here:
I've tried with index and add it in my code, I can get ride of NaN but now all values shown as 40. Can somebody please help me figure out this? (see second plot below)
idx = ~isnan(Ab);
for j=rows-1:-1:1 annotation('textbox',[pos(1)+width*(i-1),pos(2)+height*(j-1),width,height], ...
'string',num2str(Ab(idx(j,i))),'LineStyle','none','HorizontalAlignment','center',...
'VerticalAlignment','middle');
end
end
Thanks, Raydo

Best Answer

Dear all I found a solution, I just simply delete any string that contains NaN. See updated code below:
figure
H = pcolor(xedges,yedges,Ab);
colormap(cm);
set(H, 'linestyle', 'none');
pos=get(gca,'position'); % get plot position
[rows,cols]=size(Ab); % get cols and rows size
width=pos(3)/(cols-1); % get width and height postions
height =pos(4)/(rows-1);
%create textbox annotations
for i=1:cols-1
for j=rows-1:-1:1;
annotation('textbox',[pos(1)+width*(i-1),pos(2)+height*(j-1),width,height], ...
'string',num2str(Ab(j,i)),'LineStyle','none','HorizontalAlignment','center',...
'FontSize',10,'FontWeight','Bold',...
'VerticalAlignment','middle');
end
end
delete(findall(gcf,'String','NaN')); % delete the annotation
set(gca,'Xlim',[0 30]);
set(gca,'XTick',[0:3:30]);
set(gca,'Ylim',[0 5]);
set(gca,'YTick',[0:1:5]);
set(gca,'FontSize',10,'FontWeight','Bold');
Cheers, Raydo