Optimize Code for Getting and Setting Graphics Properties
Automatically Calculated Properties
MATLAB® automatically calculates the values of some properties based on the values of other properties. For example, axis limits affect the values used for axis ticks, which, in turn, affect the axis tick labels.
Mode properties can also affect when a property is calculated. Mode properties are
paired with specific properties of an object, and their names end with the word
"Mode
". For example, axes objects have an XTick
property and a XTickMode
property. If the
XTickMode
property has a value of "auto"
,
MATLAB updates the XTick
property when conditions (such as the
x-axis limits) change. If the XTickMode
property has a value of "manual
", the XTick
property is preserved regardless of other property values.
MATLAB can also calculate the values of certain properties based on other objects. 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.
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 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.
Object | Properties | When MATLAB Calculates these Properties |
---|---|---|
Axes |
| When the corresponding mode property is set to
|
| When the | |
OuterPosition | When the | |
TightInset | Always | |
| When the corresponding mode property is set to
| |
| When the corresponding mode property is set to
| |
| When the corresponding mode property is set to
| |
| When the corresponding mode property is set to
| |
Text |
| Always |
| When the text object is used as an axes title, subtitle, axis
label, or secondary label and the | |
| When the text object is used as an axes title, subtitle, axis
label, or secondary label and the corresponding mode property is set to
|
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 a calculated 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 both the Position
and Extent
properties of a text
object and then sets the Position
property.
Code with Poor Performance | Code 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 performance is better because this code:
|
Using Text Extent
to Rotate Labels
In cases where you query the text Extent
property to rotate axes
labels, it is more efficient to use the axes properties XTickLabelRotation
,
YTickLabelRotation
, and
ZTickLabelRotation
.