Main Content

cylinder

Create cylinder

Description

Create Cylinder Data

[X,Y,Z] = cylinder returns three 2-by-21 matrices containing the x-, y-, and z- coordinates of a cylinder without drawing it. The cylinder has a radius of 1 and 20 equally spaced points around its circumference. The bases are parallel to the xy-plane.

To draw the cylinder, pass X, Y, and Z to the surf or mesh function.

example

[X,Y,Z] = cylinder(r) returns the x-, y-, and z- coordinates of a cylinder with the specified profile curve, r, and 20 equally spaced points around its circumference. The function treats each element in r as a radius at equally spaced heights along the unit height of the cylinder. The size of each coordinate matrix is m-by-21, where m=numel(r). However, if r is a scalar, then m=2.

example

[X,Y,Z] = cylinder(r,n) returns the x-, y-, and z- coordinates of a cylinder with the specified profile curve, r, and n equally spaced points around its circumference. The size of each coordinate matrix is m-by-(n+1), where m=numel(r). However, if r is a scalar, then m=2.

Plot Cylinders

cylinder(___) plots a cylinder without returning the coordinates. Use this syntax with any of the input arguments in previous syntaxes.

example

cylinder(___,Name=Value) sets properties of the cylinder plot using one or more name-value arguments. For example, you can specify the color and transparency of the cylinder. For a list of properties, see Surface Properties. (since R2024b)

cylinder(ax,___) plots into the axes specified by ax instead of the current axes. Specify the axes as the first input argument.

Examples

collapse all

Create and plot a cylinder with a radius equal to 1.

cylinder

Figure contains an axes object. The axes object contains an object of type surface.

Specify the radius of a cylinder by including the input r. Then, specify the height of the cylinder by modifying the returned Z coordinate.

Define X, Y, and Z as coordinates of a cylinder with a radius of 4.

r = 4;
[X,Y,Z] = cylinder(r);

Specify a height of 20 by modifying the Z coordinate. Plot the cylinder.

h = 20;
Z = Z*h;
surf(X,Y,Z)

Figure contains an axes object. The axes object contains an object of type surface.

Create a cylinder and use the returned coordinates to plot multiple cylinders in different locations.

Create a cylinder defined by the profile function 2 + cos(t).

t = 0:pi/10:2*pi;
r = 2 + cos(t);
[X,Y,Z] = cylinder(r);

Plot the cylinder with the base centered at the origin.

surf(X,Y,Z)

Figure contains an axes object. The axes object contains an object of type surface.

Plot two more cylinders on top of the first cylinder.

hold on
surf(X,Y,Z+1)
surf(X,Y,Z+2)

Figure contains an axes object. The axes object contains 3 objects of type surface.

Input Arguments

collapse all

Profile curve, specified as a vector. cylinder treats each element in r as a radius at equally spaced heights along the unit height of the cylinder.

Number of points around the cylinder circumference, specified as a positive whole number.

Target axes, specified as an Axes object. If you do not specify the axes, then cylinder plots into the current axes.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: cylinder(4,FaceColor="red") plots a red cylinder.

Note

The properties listed here are only a subset. For a full list, see Surface Properties.

Face color, specified as one of the values in this table.

ValueDescription
'flat'

Use a different color for each face based on the values in the CData property. First you must specify the CData property as a matrix the same size as ZData. The color value at the first vertex of each face (in the positive x and y directions) determines the color for the entire face. You cannot use this value when the FaceAlpha property is set to 'interp'.

Sample of a surface with each face a different color based on sample values in the CData property

'interp'

Use interpolated coloring for each face based on the values in the CData property. First you must specify the CData property as a matrix the same size as ZData. The color varies across each face by interpolating the color values at the vertices. You cannot use this value when the FaceAlpha property is set to 'flat'.

Sample of a surface with each face showing different interpolated coloring based on sample values in the CData property

RGB triplet, hexadecimal color code, or color name

Use the specified color for all the faces. This option does not use the color values in the CData property.

Sample of a surface with all faces shown in red

'texturemap'Transform the color data in CData so that it conforms to the surface.
'none'Do not draw the faces.

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Face transparency, specified as one of these values:

  • Scalar in range [0,1] — Use uniform transparency across all the faces. A value of 1 is fully opaque and 0 is completely transparent. Values between 0 and 1 are semitransparent. This option does not use the transparency values in the AlphaData property.

  • 'flat' — Use a different transparency for each face based on the values in the AlphaData property. The transparency value at the first vertex determines the transparency for the entire face. First you must specify the AlphaData property as a matrix the same size as the ZData property. The FaceColor property also must be set to 'flat'.

  • 'interp' — Use interpolated transparency for each face based on the values in AlphaData property. The transparency varies across each face by interpolating the values at the vertices. First you must specify the AlphaData property as a matrix the same size as the ZData property. The FaceColor property also must be set to 'interp'.

  • 'texturemap' — Transform the data in AlphaData so that it conforms to the surface.

Edge line color, specified as one of the values listed here. The default color of [0 0 0] corresponds to black edges.

ValueDescription
'none'Do not draw the edges.
'flat'

Use a different color for each edge based on the values in the CData property. First you must specify the CData property as a matrix the same size as ZData. The color value at the first vertex of each face (in the positive x and y directions) determines the color for the adjacent edges. You cannot use this value when the EdgeAlpha property is set to 'interp'.

Sample of a surface with each edge a different color based on sample values in the CData property

'interp'

Use interpolated coloring for each edge based on the values in the CData property. First you must specify the CData property as a matrix the same size as ZData. The color varies across each edge by linearly interpolating the color values at the vertices. You cannot use this value when the EdgeAlpha property is set to 'flat'.

Sample of a surface with each edge showing different interpolated coloring based on sample values in the CData property

RGB triplet, hexadecimal color code, or color name

Use the specified color for all the edges. This option does not use the color values in the CData property.

Sample of a surface with all edges shown in red

RGB triplets and hexadecimal color codes are useful for specifying custom colors.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Edge transparency, specified as one of these values:

  • Scalar in range [0,1] — Use uniform transparency across all of the edges. A value of 1 is fully opaque and 0 is completely transparent. Values between 0 and 1 are semitransparent. This option does not use the transparency values in the AlphaData property.

  • 'flat' — Use a different transparency for each edge based on the values in the AlphaData property. First you must specify the AlphaData property as a matrix the same size as the ZData property. The transparency value at the first vertex determines the transparency for the entire edge. The EdgeColor property also must be set to 'flat'.

  • 'interp' — Use interpolated transparency for each edge based on the values in AlphaData property. First you must specify the AlphaData property as a matrix the same size as the ZData property. The transparency varies across each edge by interpolating the values at the vertices. The EdgeColor property also must be set to 'interp'.

Version History

Introduced before R2006a

expand all

See Also

Functions

Properties