MATLAB Answers

How do I make a mesh or surface plot that is clear in certain places?

59 views (last 30 days)
I want to create a SURF or MESH plot but would like the surface to have empty regions or holes in specific places.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 May 2013
If the surface or mesh data has a NaN (not-a-number) in it, then the location of the NaN will be blank (just as NaNs in 2-d plots leave blanks). The example below illustrates using NaNs to make certain parts of a plot transparent.
%Generate a SURF plot with peaks data
z = peaks;
surf(z)
%Change peaks data so that a portion of that matix is made up of NaNs
m = z;
m(15:30,15:30) = nan;
%Plot the new matrix, m where the x and y information consisting of NaNs
%represents a hole in the SURF plot
figure;
surf(m)
As of MATLAB 6.0 (R12) you can use transparency to set a face to be transparent.

  0 Comments

Sign in to comment.

More Answers (2)

francesco
francesco on 9 Nov 2013
all empty, if meets a Nan doesn't plot all the column maybe convert NaN in zeros than plot ommitted them

  0 Comments

Sign in to comment.


Octavian Macari
Octavian Macari on 7 Jun 2018
Good morning,
i am trying to the the same thing but that dont work well
<<
>>
The surface orange is the surface i want to cut with the boundar (represented with blue or orange point)
if i gave nan to the orange point the resulte is the colored surface(blue surface), for that i used the scrip below
How can i remove juts part of surface is outside of blue line?
script: x = DD(nf_p,1) ; y = DD(nf_p,2) ; z = DD(nf_p,3)*(-1) ; x0 = min(x) ; x1 = max(x) ; y0 = min(y) ; y1 = max(y) ; N = 100; xl = linspace(x0,x1,N) ; yl = linspace(y0,y1,N) ; [X,Y] = meshgrid(xl,yl) ;
P0 = [x,y] ; V = z ; DF=[DD(fronteira,1) DD(fronteira,2)];s=size(DF); na=nan(s(1),1); P0=[P0;DF];V=[V;na];
F = scatteredInterpolant(P0,V,'natural','none') ; pq = [X(:),Y(:)] ; vq = F(pq) ; Z = vq ; Z = reshape(Z,size(X)) ;

  0 Comments

Sign in to comment.

Sign in to answer this question.