MATLAB Answers

surf command doesn't plot the value next to NaNs values

2 views (last 30 days)
Valeria Leto
Valeria Leto on 14 Oct 2021
Commented: Valeria Leto on 16 Oct 2021
Hi! I don't understand why the surf command doesn't plot the value next to NaNs values in the matrix M_array. For example the second pixel of the first row is 124, but it doesn't appear.
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);

Accepted Answer

Dave B
Dave B on 14 Oct 2021
Edited: Dave B on 14 Oct 2021
If you think of surf as defining a bunch of little rectangles, when one vertex is missing you can't draw the rectangle.
Your description makes surf sound like a 2-D plot (the second pixel is 124), but really it's a 3-D plot, the second face in the top row is a quadrilateral that has z values of 124, 123, NaN and 102
If you only intend to look at this in 2d, you could set the z values to all be 1 (or 0, or whatever):
load M_array
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);
surf(c,d,ones(size(M_array)),M_array)
view(2)
colormap gray
colorbar
Or just use imagesc or pcolor which are intended for the 2d case!
pcolor(c,d,M_array)
colormap gray
colorbar
  2 Comments
Dave B
Dave B on 14 Oct 2021
I'm sure I'm going to regret disagreeing with @Walter Roberson, but I was under the impression that the default coloring of surf uses the 'first' color for each face, where first is defined in positive x and y directions:
surf([1 2 3 4;5 6 7 8;9 10 11 12])
view(2)
colorbar
colormap(lines(7))
caxis([.5 7.5])
(Also note in my snippet in the original answer, using the NaN containing data for C and a matrix of ones for Z was sufficient to prevent the spread of a NaN vertex to its adjacent faces)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 14 Oct 2021
How about if you just interpolate over the nans with the regionfill() function?
load M_array
% Fill nan holes in M_array.
nanMap = isnan(M_array);
M_arrayRepaired = regionfill(M_array, nanMap)
% Rest of code:
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);
surf(c,d,ones(size(M_arrayRepaired)),M_arrayRepaired)
view(2)
colormap gray
colorbar
  3 Comments
Valeria Leto
Valeria Leto on 16 Oct 2021
@Image Analyst thank you for pointing that out. Anyway if I use
surf(c,d,ones(size(M_array)),M_array)
I specify in c and d the position of M_array values, so it's ok.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by