Displaying surface with non-rectangular boundary

2.1k views Asked by At

Refer to the attached image. I want to display the image in Matlab using the function surf(). However, I only want to display the region of the actual object without the background (the pale-green region surrounding the actual object which has 0 value). How to do that? I tried replacing all the outer region with 0 with NaN and setting all the values in the height map to a nonzero value, but still I get an error message:

Subscript indices must either be real positive integers or logicals.

So how can I display a surface that has a non-rectangular boundary?

enter image description here

1

There are 1 answers

0
Luis Mendo On BEST ANSWER

Setting those values to NaN should do. Here's an example:

[x, y] = ndgrid(linspace(-1,1,500));
z = cos(2*pi*(x+y)*2);
z(x.^2+y.^2>1) = NaN; %// remove values outside unit circle
surf(x,y,z,'edgecolor','none')
colorbar
view(2)
axis equal

enter image description here