This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Getting and Setting Properties

Automatically Calculated Properties

Certain properties have dependencies on the value of other properties. MATLAB® automatically calculates the values of these properties and updates their values based on the current graphics model. For example, axis limits affect the values used for axis ticks, which, in turn, affect the axis tick labels.

When you query a calculated property, MATLAB performs an implicit drawnow to ensure all property values are up to date before returning the property value. The query causes a full update of all dependent properties and an update of the screen.

MATLAB calculates the values of certain properties based on other values on which that property depends. For example, plotting functions automatically create an axes with axis limits, tick labels, and a size appropriate for the plotted data and the figure size.

MATLAB graphics performs a full update, if necessary, before returning a value from a calculated property to ensure the returned value is up to date.

ObjectAutomatically Calculated Properties

Axes

CameraPosition, CameraTarget, CameraUpVector, CameraViewAngle

 

Position, OuterPosition, TightInset

 

XLim, YLim, ZLim

 

XTick, YTick, ZTick, XMinorTick, YMinorTick, ZMinorTick

 

XTickLabel, YTickLabel, ZTickLabel, TickDir

 

SortMethod

Text

Extent

Inefficient Cycles of Sets and Gets

When you set property values, you change the state of the graphics model and mark it as needing to be updated. When you query an autocalculated property, MATLAB needs to perform an update if the graphics model and graphics hardware are not in sync.

When you get and set properties in the same loop, you can create a situation where updates are performed with every pass through the loop.

  • The get causes an update.

  • The set marks the graphics model as needing an update.

The cycle is repeated with each pass through the loop. It is better to execute all property queries in one loop, then execute all property sets in another loop, as shown in the following example.

This example gets and sets the text Extent property.

Code with Poor PerformanceCode with Better Performance
h = gobjects(1,500);
p = zeros(500,3);
for ix = 1:500
   h(ix) = text(ix/500,ix/500,num2str(ix));
end
drawnow

% Gets and sets in the same loop,
% prompting a full update at each pass
for ix = 1:500
   pos = get(h(ix),'Position');
   ext = get(h(ix),'Extent');
   p(ix,:) = [pos(1)+(ext(3)+ext(1)), ...
              pos(2)+ext(2)+ext(4),0];
   set(h(ix),'Position',p(ix,:))
end
drawnow
h = gobjects(1,500);
p = zeros(500,3);
for ix = 1:500
   h(ix) = text(ix/500,ix/500,num2str(ix));
end
drawnow

% Get and save property values
for ix=1:500
   pos = get(h(ix),'Position');
   ext = get(h(ix),'Extent');
   p(ix,:) = [pos(1)+(ext(3)+ext(1)), ...
              pos(2)+ext(2)+ext(4),0];
end

% Set the property values and 
% call a drawnow after the loop
for ix=1:500
   set(h(ix),'Position',p(ix,:));
end
drawnow

This code performs poorly because:

  • The Extent property depends on other values, such as screen resolution, figure size, and axis limits, so querying this property can cause a full update.

  • Each set of the Position property makes a full update necessary when the next get of the Extent property occurs.

The performance is better because this code:

  • Queries all property values in one loop and stores these values in an array.

  • Sets all property values in a separate loop.

  • Calls drawnow after the second loop finishes.

Changing Text Extent to Rotate Labels

In cases where you change the text Extent property to rotate axes labels, it is more efficient to use the axes properties XTickLabelRotation, YTickLabelRotation, and ZTickLabelRotation.