Main Content

Optimize Code for Getting and Setting Graphics 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.

This table lists some of the more commonly calculated properties.

ObjectPropertiesWhen MATLAB Calculates these Properties

Axes

CameraPosition, CameraTarget, CameraUpVector, CameraViewAngle

Always

 

Position, OuterPosition, TightInset

Always

 

XLim, YLim, ZLim

Always

 

XTick, YTick, ZTick, XMinorTick, YMinorTick, ZMinorTick

Always

 

XTickLabel, YTickLabel, ZTickLabel, TickDir

Always

 

SortMethod

Always

Text

Extent

Always

 

Position

Only when the text object is used as an axes title or an axis label

 

FontSize, FontWeight

Only when the text object is used as an axes title or an axis label

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.