File Exchange

image thumbnail

version (2.09 MB) by Massimo Ciacci
Make surface plot when X,Y,Z contain NaNs, taking care of NaN-discontinuities


Updated 09 Oct 2019

View License

% NANSURF(X,Y,Z,varargin) - makes a trisurf plot of X,Y,Z choosing a triangulation
% that minimizes zigzag edges due to NaN's in Z. No data interpolation takes place.
% inputs : X,Y,Z 2D matrixes representing a surface
% outputs: ph plot handle
% trgList the chosen triangulation
% NANSURF(X,Y,Z) plots the colored parametric surface defined by matrix arguments.
% it works the same as trisurf but choosing triangulation according to NaN's in Z.
% NANSURF(X,Y,Z,'PropertyName',PropertyValue,...) plots the data
% stored in the vectors X and Y, according to Property/Value pairs:
% QuadEdgesOnly
% 1 : only show quad edges as in surf, hides the triangle edges
% 0 : (Default) plots all triangle edges (can be changed outside with 'edgecolor')
% trgExtend
% 1 : (Default) enable extending triangles to better reduce NaN-edge-aliasing
% in close to horizonal/vertical lines.
% 0 : maximizes nr of rendered triangles without creating/extending them
% should only be used to compare/debug triangulation
% DBGTrg
% 1 : debug plot showing which triangles are affected by NaNs
% 0 : (Default) no debug plots
% When a face, e.g. quadruple of vertexes in a meshgrid, contain 1 Nan, surf does not render it.
% Choose triangles to maximize # rendered ones, maintaining correct face orientation.
% Correct face orientation (based on gradient) improves color rendering.
% (1) NaNsurf makes a triangulation which maximizes the number of valid triangles
% a valid triangle contains no NaNs. output: TRG, a list of L/R triangles based on NaNs in Z
% (2) TRG list from step (1) is used as input to trisurf / patch: trisurf(TRG,X,Y,Z)
% plotSpec = {'edgecolor','k','facecolor','interp'};
% NaNsurf(X,Y,Z,plotSpec{:});
% NaNsurf(X,Y,Z,plotSpec{:},'trgExtend',1,'DBGTrg',1);
% M = 22; N = 34;
% Mh = (M+1)/2; Nh = (N+1)/2;
% [X,Y] = meshgrid((1:N)-Nh,(1:M)-Mh);
% Rn = sqrt((X/Nh).^2+(Y/Mh).^2);
% Z = sin(3*pi*Rn)./(3*pi*Rn);
% Z(find(X.^2+Y.^2 > Mh^2)) = NaN;
% NaNsurf(X,Y,Z,'edgecolor','k','facecolor','interp')
% axis([-Mh Mh -Nh Nh -.5 1])
% rev.1.8, Massimo Ciacci, September 20, 2019

Cite As

Massimo Ciacci (2019). (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

@ Marco, relating to "is it possible to not draw the inner edge between the two triangles of the same quad?"
This was pretty easy by setting the edgecolor to 'none' and adding a Surf command with only the edges and facecolor set to 'none'.
I attempted resolving some asymmetry in display but they only work with the surf command and not with patch/trisurf.
Nevertheless the added function "reorder_XYZT_for_Surf_Symmetry", based on Bruno Luong answer on 17/Nov,2018 in
%, can be helpful to plot flat shaded surfaces without color asymmetries.

Very good job! Only one observation: is it possible to not draw the inner edge between the two triangles of the same quad? It would produce a visualization closer to Matlab built-in surf function.



Tested with ML2016b, updated screenshot

- Added option 'QuadEdgesOnly'
- Added method reorder_XYZT_for_Surf_Symmetry for preprocessing surf data

a preview button would have been nice..

oops, had forgotten the zip file..

embedded one debug-plot function were it belongs
slightly edited help section
changed screen-shoot to a simpler one

Yet a little formatting of the help section.

Changed screenshot, improved help to NanBasedTriangulation and placefig.

some minor comment editing


MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags