Documentation Center

  • Trial Software
  • Product Updates

Setting and Querying Property Values

Using set and get

The set and get functions specify and retrieve the value of existing graphics object properties. They also enable you to list possible values for properties that have a fixed set of values. (You can also use the Property Editor to set many property values.)

The basic syntax for setting the value of a property on an existing object is

set(object_handle,'PropertyName','NewPropertyValue')

To query the current value of a specific object's property, use a statement like

returned_value = get(object_handle,'PropertyName');

Property names are always quoted strings. Property values depend on the particular property.

See Accessing Object Handles and the findobj command for information on finding the handles of existing objects.

Setting Property Values

Change the properties of an existing object using the set function and the handle returned by the creating function. For example, this statement moves the y-axis to the right side of the plot on the current axes:

set(gca,'YAxisLocation','right')

If the handle argument is a vector, MATLAB® sets the specified value on all identified objects.

Specify property names and property values using structure arrays or cell arrays. This can be useful if you want to set the same properties on a number of objects. For example, define a structure to set axes properties appropriately to display a particular graph:

view1.CameraViewAngleMode = 'manual';
view1.DataAspectRatio = [1 1 1];
view1.Projection = 'Perspective';

To set these values on the current axes, type

set(gca,view1)

Listing Possible Values

Use set to display the possible values for many properties without actually assigning a new value. For example, this statement obtains the values you can specify for line object markers:

set(obj_handle,'Marker')

MATLAB returns a list of values for the Marker property for the type of object specified by obj_handle. Braces indicate the default value:

[ + | o | * | . | x | square | diamond | v | ^ | > | < | 
pentagram | hexagram | {none} ]

To see a list of all settable properties along with possible values of properties that accept string values, use set with just an object handle:

set(object_handle) 

For example, for a surface object, MATLAB returns

CData
CDataScaling: [ {on} | off]
EdgeColor: [ none | {flat} | interp ] ColorSpec.
EraseMode: [ {normal} | background | xor | none ]
FaceColor: [ none | {flat} | interp | texturemap ] ColorSpec.
LineStyle: [ {-} | -- | : | -. | none ]
  . 
  . 
  .
Visible: [ {on} | off ]

If you assign the output of the set function to a variable, MATLAB returns the output as a structure array:

a = set(gca);

The field names in a are the object's property names and the field values are the possible values for the associated property:

a.GridLineStyle
ans = 

    '-'
    '--'
    ':'
    '-.'
    'none'

returns the possible values for the axes grid line styles. While property names are not case sensitive, MATLAB structure field names are:

a.gridlinestyle
??? Reference to non-existent field 'gridlinestyle'.

returns an error.

Querying Property Values

Use get to query the current value of a property or of all the object's properties. For example, check the value of the current axes PlotBoxAspectRatio property:

get(gca,'PlotBoxAspectRatio')
ans =
     1     1     1

MATLAB lists the values of all properties, where practical. However, for properties containing data, MATLAB lists the dimensions only (for example, CurrentPoint and ColorOrder).

AmbientLightColor = [1 1 1]
Box = off
CameraPosition = [0.5 0.5 2.23205]
CameraPositionMode = auto
CameraTarget = [0.5 0.5 0.5]
CameraTargetMode = auto
CameraUpVector = [0 1 0]
CameraUpVectorMode = auto
CameraViewAngle = [32.2042]
CameraViewAngleMode = auto
CLim: [0 1]
CLimMode: auto
Color: [0 0 0]
CurrentPoint: [ 2x3  double]
ColorOrder: [ 7x3  double]
  . 
  . 
  .
Visible = on

Querying Individual Properties

You can obtain the data from the property by getting that property individually:

get(gca,'ColorOrder')
ans =
         0         0    1.0000
         0    0.5000         0
    1.0000         0         0
         0    0.7500    0.7500
    0.7500         0    0.7500
    0.7500    0.7500         0
    0.2500    0.2500    0.2500

Returning a Structure

If you assign the output of get to a variable, MATLAB creates a structure array whose field names are the object property names and whose field values are the current values of the named property.

For example, if you plot some data, x and y,

h = plot(x,y);

and get the properties of the line object created by plot,

a = get(h);

you can access the values of the line properties using the field name. This call to the text command places the string 'x and y data' at the first data point and colors the text to match the line color:

text(x(1),y(1),'x and y data','Color',a.Color)

If x and y are matrices, plot draws one line per column. To label the plot of the second column of data, reference that line:

text(x(1,2),y(1,2),'Second set of data','Color',a(2).Color)

Querying Groups of Properties

You can define a cell array of property names and conveniently use it to obtain the values for those properties. For example, suppose you want to query the values of the axes "camera mode" properties. First, define the cell array:

camera_props(1) = {'CameraPositionMode'};
camera_props(2) = {'CameraTargetMode'};
camera_props(3) = {'CameraUpVectorMode'};
camera_props(4) = {'CameraViewAngleMode'};

Use this cell array as an argument to obtain the current values of these properties:

get(gca,camera_props)
ans = 
     'auto' 'auto' 'auto' 'auto'
Was this topic helpful?