| MATLAB Functions | Help Desk |
surface
Create Surface objectsurface(Z) surface(Z,C) surface(X,Y,Z) surface(X,Y,Z,C) surface(...'PropertyName',PropertyValue,...) h = surface(...)
surface is the low-level function for creating Surface graphics objects. Surfaces are plots of matrix data created using the row and column indices of each element as the x- and y- coordinates and the value of each element as the z-coordinate.
surface(Z)
plots the Surface specified by the matrix Z. Here, Z is a single-valued function, defined over a geometrically rectangular grid.
surface(Z,C)
plots the Surface specified by Z and colors it according to the data in C (see "Examples").
surface(X,Y,Z,C)
plots the parametric surface specified by X, Y and Z, with color specified by C.
surface(X,Y,Z)
uses C = Z, so color is proportional to surface height above the x-y plane.
surface(x,y,Z), surface(x,y,Z,C)
replaces the first two matrix arguments with vectors and must have length(x) = n and length(y) = m where [m,n] = size(Z). In this case, the vertices of the Surface facets are the triples (x(j),y(i),Z(i,j)). Note that x corresponds to the columns of Z and y corresponds to the rows of Z. For a complete discussion of parametric surfaces, see the surf reference page.
surface(...'PropertyName',PropertyValue,...)
follows the X, Y, Z, and C arguments with property name/property value pairs to specify additional Surface properties. These properties are described in the "Surface Properties" section.
h = surface(...)
returns a handle to the created Surface object.
Unlike high-level area creation functions, such as surf or mesh, surface does not respect the settings of the Figure and Axes NextPlot properties. It simply adds the Surface object to the current Axes.
If you do not specify separate color data (C), MATLAB uses the matrix (Z) to determine the coloring of the Surface. In this case, color is proportional to values of Z. You can specify a separate matrix to color the Surface independently of the data defining the area of the Surface.
You can specify properties as property name/property value pairs, structure arrays, and cell arrays (see the set and get reference pages for examples of how to specify these data types).
surface provides convenience forms that allow you to omit the property name for the XData, YData, ZData, and CData properties. For example,
surface('XData',X,'YData',Y,'ZData',Z,'CData',C)
is equivalent to:
surface(X,Y,Z,C)When you specify only a single matrix input argument,
surface(Z)MATLAB assigns the data properties as if you specified,
surface('XData',[1:size(Z,2)],...
'YData',[1:size(Z,1)],...
'ZData',Z,...
'CData',Z)
The axis, caxis, colormap, hold, shading, and view commands set graphics properties that affect Surfaces. You can also set and query Surface property values after creating them using the set and get commands.
This example creates a Surface using the peaks M-file to generate the data and colors it using the clown Image. The ZData is a 49-by-49 element matrix, while the CData is a 200-by-320 matrix. You must set the FaceColor to texturemap to use ZData and CData of different dimensions.
load clown surface(peaks,flipud(X),... 'FaceColor','texturemap',... 'EdgeColor','none',... 'CDataMapping','direct') colormap(map) view(3)
Note the use of the surface(Z,C) convenience form combined with property name/property value pairs.
Since the clown data (X) is typically viewed as an Image, which MATLAB normally displays with 'ij' axis numbering and direct CDataMapping, this example reverses the data in the vertical direction using flipud and sets the CDataMapping property to direct.
Setting Default Properties
You can set default Surface properties on the Axes, Figure, and Root levels:
set(0,'DefaultSurfaceProperty',PropertyValue...) set(gcf,'DefaultSurfaceProperty',PropertyValue...) set(gca,'DefaultSurfaceProperty',PropertyValue...)Where Property is the name of the Surface property whose default value you want to set and
PropertyValue is the value you are specifying.
This section lists property names along with the type of values each accepts. Curly braces { } enclose default values.
AmbientStrength scalar >= 0 and <= 1
Strength of ambient light. This property sets the strength of the ambient light, which is a nondirectional light source that illuminates the entire scene. You must have at least one visible Light object in the Axes for the ambient light to be visible. The Axes AmbientColor property sets the color of the ambient light, which is therefore the same on all objects in the Axes.
DiffuseStrength and SpecularStrength properties.
BusyAction cancel | {queue}
Callback routine interruption. The BusyAction property enables you to control how MATLAB handles events that potentially interrupt executing callback routines. If there is a callback routine executing, subsequently invoked callback routes always attempt to interrupt it. If the Interruptible property of the object whose callback is executing is set to on (the default), then interruption occurs at the next point where the event queue is processed. If the Interruptible property is off, the BusyAction property (of the object owning the executing callback) determines how MATLAB handles the event. The choices are:
cancel - discard the event that attempted to execute a second callback routine.
queue - queue the event that attempted to execute a second callback routine until the current callback finishes.
ButtonDownFcn stringButton press callback routine. A callback routine that executes whenever you press a mouse button while the pointer is over the Surface object. Define this routine as a string that is a valid MATLAB expression or the name of an M-file. The expression executes in the MATLAB workspace.
CData matrix
Vertex colors. A matrix of values that specify the color at every point in ZData. If you set the FaceColor property to texturemap, CData does not need to be the same size as ZData. In this case, MATLAB maps CData to conform the Surface defined by ZData.
caxis) or interpreted directly as indices into the colormap, depending on the setting of the CDataMapping property.
True color defines an RGB value for each vertex. If the coordinate data (XData for example) are contained in an m-by-n matrix, then CData must be an m-by-n-3 array. The first page contains the red components, the second the green components, and the third the blue components of the colors.
On computer displays that cannot display true color (e.g., 8-bit displays), MATLAB uses dithering to approximate the RGB triples using the colors in the Figure's Colormap and Dithermap, which defaults to colorcube(64). You can also specify your own dithermap.
CDataMapping {scaled} | direct
Direct or scaled color mapping. This property determines how MATLAB interprets indexed color data used to color the Surface. (If you use true color specification for CData, this property has no effect.)
scaled - transform the color data to span the portion of the colormap indicated by the Axes CLim property, linearly mapping data values to colors. See the caxis reference page for more information on this mapping.
direct - use the color data as indices directly into the colormap. The color data should then be integer values ranging from 1 to length(colormap). MATLAB maps values less than 1 to the first color in the colormap, and values greater than length(colormap) to the last color in the colormap. Values with a decimal portion are fixed to the nearest, lower integer.
Children matrix of handlesAlways the empty matrix; Surface objects have no children.
Clipping {on} | off
Clipping to Axes rectangle. When Clipping is on, MATLAB does not display any portion of the Surface that is outside the Axes rectangle.
CreateFcn stringCallback routine executed during object creation. This property defines a callback routine that executes when MATLAB creates a Surface object. You must define this property as a default value for Surfaces. For example, the statement,
set(0,'DefaultSurfaceCreateFcn',... 'set(gcf,''DitherMap'',my_dithermap)')defines a default value on the Root level that sets the Figure
DitherMap property whenever you create a Surface object. MATLAB executes this routine after setting all Surface properties. Setting this property on an existing Surface object has no effect.
The handle of the object whose CreateFcn is being executed is accessible only through the Root CallbackObject property, which can be queried using gcbo.
DeleteFcn string
Delete Surface callback routine. A callback routine that executes when you delete the Surface object (e.g., when you issue a delete command or clear the Axes or Figure). MATLAB executes the routine before destroying the object's properties so these values are available to the callback routine.
The handle of the object whose DeleteFcn is being executed is accessible only through the Root CallbackObject property, which can be queried using gcbo.
DiffuseStrength scalar >= 0 and <= 1Intensity of diffuse light. This property sets the intensity of the diffuse component of the light falling on the Surface. Diffuse light comes from Light objects in the Axes.
You can also set the intensity of the ambient and specular components of the light on the Surface object. See theAmbientStrength and SpecularStrength properties.
EdgeColor {ColorSpec} | none | flat | interpColor of the Surface edge. This property determines how MATLAB colors the edges of the individual faces that make up the Surface:
ColorSpec -- A three-element RGB vector or one of MATLAB's predefined names, specifying a single color for edges. The default EdgeColor is black. See the ColorSpec reference page for more information on specifying color.
none -- Edges are not drawn.
flat -- The CData value of the first vertex for a face determines the color of each edge:
interp -- Linear interpolation of the CData values at the face vertices determines the edge color.
EdgeLighting {none} | flat | gouraud | phongAlgorithm used for lighting calculations. This property selects the algorithm used to calculate the effect of Light objects on Patch edges. Choices are:
none - Lights do not affect the edges of this object.
flat - The effect of Light objects is uniform across each edge of the Surface.
gouraud - The effect of Light objects is calculated at the vertices and then linearly interpolated across the edge lines.
phong - The effect of Light objects is determined by interpolating the vertex normals across each edge line and calculating the reflectance at each pixel. Phong lighting generally produces better results than Gouraud lighting, but takes longer to render.
EraseMode {normal} | none | xor | backgroundErase mode. This property controls the technique MATLAB uses to draw and erase Surface objects. Alternative erase modes are useful in creating animated sequences, where control of the way individual objects redraw is necessary to improve performance and obtain the desired effect.
normal -- Redraw the affected region of the display, performing the three-dimensional analysis necessary to ensure that all objects are rendered correctly. This mode produces the most accurate picture, but is the slowest. The other modes are faster, but do not perform a complete redraw and are therefore less accurate.
none -- Do not erase the Surface when it is moved or destroyed.
xor -- Draw and erase the Surface by performing an exclusive OR (XOR) with each pixel index of the screen beneath it. Erasing the Surface does not damage the color of the objects beneath it. However, Surface color depends on the color of the screen beneath it and is correctly colored only when over the Axes background color, or Figure background color if the Axes color is set to none.
background -- Erase the Surface by drawing it in the Axes' background color. This damages objects that are behind the erased object, but Surface objects are always properly colored.
FaceColor ColorSpec | none | {flat} | interpColor of the Surface face. This property can be any of the following:
ColorSpec -- A three-element RGB vector or one of MATLAB's predefined names, specifying a single color for faces. See the ColorSpec reference page for more information on specifying color.
none -- Do not draw faces. Note that edges are drawn independently of faces.
flat -- The values of CData determine the color for each face of the Surface. The color data at the first vertex determines the color of the entire face.
interp -- Bilinear interpolation of the values at each vertex (the CData) determines the coloring of each face.
texturemap -- Texture map the CData to the Surface. MATLAB transforms the color data so that it conforms to the Surface. (See "Examples")
FaceLighting {none} | flat | gouraud | phongAlgorithm used for lighting calculations. This property selects the algorithm used to calculate the effect of Light objects on the Surface. Choices are:
none - Lights do not affect the faces of this object.
flat - The effect of Light objects is uniform across the faces of the Surface. Select this choice to view faceted objects.
gouraud - The effect of Light objects is calculated at the vertices and then linearly interpolated across the faces. Select this choice to view curved surfaces.
phong - The effect of Light objects is determined by interpolating the vertex normals across each face and calculating the reflectance at each pixel. Select this choice to view curved surfaces. Phong lighting generally produces better results than Gouraud lighting, but takes longer to render.
HandleVisibility {on} | callback | off
Control access to object's handle by command-line users and GUIs. This property determines when an object's handle is visible in its parent's list of children. Handles are always visible when HandleVisibility is on. When HandleVisibility is callback, handles are visible from within callbacks or functions invoked by callbacks, but not from within functions invoked from the command line - a useful way to protect GUIs from command-line users, while permitting their callbacks complete access to their own handles. Setting HandleVisibility to off makes handles invisible at all times - which is occasionally necessary when a callback needs to invoke a function that might potentially damage the UI, and so wants to temporarily hide its own handles during the execution of that function.
callback or off, the object's handle does not appear in its parent's Children property, Figures do not appear in the Root's CurrentFigure property, objects do not appear in the Root's CallbackObject property or in the Figure's CurrentObject property, and Axes do not appear in their parent's CurrentAxes property.
The Root ShowHiddenHandles property can be set to on to temporarily make all handles visible, regardless of their HandleVisibility settings (this does not affect the values of the HandleVisibility properties).
Handles that are hidden are still valid. If you know an object's handle, you can set and get its properties, and pass it to any function that operates on handles. This property is useful for preventing command-line users from accidently drawing into or deleting a Figure that contains only user interface devices (such as a dialog box).
Interruptible {on} | off
Callback routine interruption mode. The Interruptible property controls whether a Surface callback routine can be interrupted by subsequently invoked callback routines. Only callback routines defined for the ButtonDownFcn are affected by the Interruptible property. MATLAB checks for events that can interrupt a callback routine only when it encounters a drawnow, figure, getframe, or pause command in theroutine. See the EventQueue property for related information.
LineStyle {-} | -- | : | -. | none
Edge line type. This property determines the line style used to draw Surface edges. The available line styles are:
|
|---|
LineWidth scalarEdge line width. The width of the lines in points used to draw Surface edges. The default width is 0.5 points (1 point = 1/72 inch).
Marker marker symbol (see table)
Marker symbol. The Marker property specifies symbols that display at vertices. You can set values for the Marker property independently from the LineStyle property.
|
|---|
MarkerEdgeColor ColorSpec | none | {auto}Marker edge color. The color of the marker or the edge color for filled markers (circle, square, diamond, pentagram, hexagram, and the four triangles).
ColorSpec defines a single color to use for the edge (see the ColorSpec reference page).
none specifies no color, which makes nonfilled markers invisible.
auto uses the same color as the EdgeColor property.
MarkerFaceColor ColorSpec | {none} | autoMarker face color. The fill color for markers that are closed shapes (circle, square, diamond, pentagram, hexagram, and the four triangles).
ColorSpec defines a single color to use for all marker on the Surface (see the ColorSpec reference page).
none makes the interior of the marker transparent, allowing the background to show through.
auto uses the CData for the vertex located by the marker to determine the color.
MarkerSize size in points.
Marker size. A scalar specifying the marker size, in points. The default value for MarkerSize is six points (1 point = 1/72 inch). Note that MATLAB draws the point marker at 1/3 the specified marker size.
MeshStyle {both} | row | columnRow and column lines. This property specifies whether to draw all edge lines or just row or column edge lines.
both draws edges for both rows and columns.
row draws row edges only.
column draws column edges only.
NormalMode {auto} | manual
MATLAB-generated or user-specified normal vectors. When this property is auto, MATLAB calculates vertex normals based on the coordinate data. If you specify your own vertex normals, MATLAB sets this property to manual and does not generate its own data. See also the VertexNormals property.
Parent handleSurface's parent object. The parent of a Surface object is the Axes in which it is displayed. You can move a Surface object to another Axes by setting this property to the handle of the new parent.
Selected on | off
Is object selected. When this property is on. MATLAB displays a dashed bounding box around the Surface if the SelectionHighlight property is also on. You can, for example, define the ButtonDownFcn to set this property, allowing users to select the object with the mouse.
SelectionHighlight {on} | off
Objects highlight when selected. When the Selected property is on, MATLAB indicates the selected state by drawing a dashed bounding box around the Surface. When SelectionHighlight is off, MATLAB does not draw the handles.
SpecularColorReflectancescalar in the range 0 to 1
Color of specularly reflected light. When this property is 0, the color of the specularly reflected light depends on both the color of the object from which it reflects and the color of the light source. When set to 1, the color of the specularly reflected light depends only on the color or the light source (i.e., the Light object Color property). The proportions vary linearly for values in between.
SpecularExponent scalar >= 1Harshness of specular reflection. This property controls the size of the specular spot. Most materials have exponents in the range of 5 to 20.
SpecularStrength scalar >= 0 and <= 1Intensity of specular light. This property sets the intensity of the specular component of the light falling on the Surface. Specular light comes from Light objects in the Axes.
You can also set the intensity of the ambient and diffuse components of the light on the Surface object. See theAmbientStrength and DiffuseStrength properties. Also see the material function.
Tag string
User-specified object label. The Tag property provides a means to identify graphics objects with a user-specified label. This is particularly useful when constructing interactive graphics programs that would otherwise need to define object handles as global variables or pass them as arguments between callback routines. You can define Tag as any string.
Type string (read only)
Class of the graphics object. The class of the graphics object. For Surface objects, Type is always the string 'surface'.
UserData matrix
User-specified data. Any matrix you want to associate with the Surface object. MATLAB does not use this data, but you can access it using the set and get commands.
VertexNormals vector or matrixSurface normal vectors. This property contains the vertex normals for the Surface. MATLAB generates this data to perform lighting calculations. You can supply your own vertex normal data, even if it does not match the coordinate data. This can be useful to produce interesting lighting effects.
Visible {on} | off
Surface object visibility. By default, all Surfaces are visible. When set to off, the Surface is not visible, but still exists and you can query and set its properties.
XData vector or matrix
X-coordinates. The x-position of the surface points. If you specify a row vector, surface replicates the row internally until it has the same number of columns as ZData.
YData vector or matrix
Y-coordinates. The y-position of the surface points. If you specify a row vector, surface replicates the row internally until it has the same number of rows as ZData.
ZData vector or matrixZ-coordinates. Z-position of the surface points. See the "Description" section for more information.
ColorSpec, mesh, patch, pcolor, surf