Optimize Code for Getting and Setting Graphics Properties - MATLAB & Simulink (original) (raw)
Main Content
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 theXTickMode
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 | CameraPosition,CameraTarget, CameraUpVector,CameraViewAngle | When the corresponding mode property is set to"auto" |
Position, InnerPosition, | When the PositionConstraint property is set to"outerposition" | |
OuterPosition | When the PositionConstraint property is set to"innerposition" | |
TightInset | Always | |
XLim, YLim,ZLim | When the corresponding mode property is set to"auto" | |
XTick, YTick,ZTick, XMinorTick,YMinorTick,ZMinorTick | When the corresponding mode property is set to"auto" | |
XTickLabel, YTickLabel,ZTickLabel,TickDir | When the corresponding mode property is set to"auto" | |
SortMethod | When the corresponding mode property is set to"auto" | |
Text | Extent | Always |
Position | When the text object is used as an axes title, subtitle, axis label, or secondary label and the PositionMode property is set to "auto" | |
FontSize,FontWeight | When the text object is used as an axes title, subtitle, axis label, or secondary label and the corresponding mode property is set to"auto" |
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 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. |
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, andZTickLabelRotation.