Documentation Center

  • Trial Software
  • Product Updates

Setting Default Property Values

Factory- and User-Defined Values

All object properties have values built into MATLAB® (i.e., factory-defined values). You can also define your own default values at any point in the object hierarchy.

You cannot define default values for plot objects.

How MATLAB Searches for Default Values

MATLAB searches for a default value beginning with the current object and continuing through the object's ancestors until it finds a user-defined default value or until it reaches the factory-defined value. Therefore, a search for property values is always satisfied.

The closer to the root of the hierarchy you define the default, the broader its scope. If you specify a default value for line objects on the root level, MATLAB uses that value for all lines because the root is at the top of the hierarchy. If you specify a default value for line objects on the axes level, MATLAB uses that value for line objects drawn only in that axes.

If you define default values on more than one level, the value defined on the closest ancestor takes precedence because MATLAB terminates the search as soon as it finds a value.

Setting default values affects only those objects created after you set the default. Existing graphics objects are not affected.

This diagram shows the steps MATLAB follows in determining the value of a graphics object property.

Defining Default Values

To specify default values, create a string beginning with the word Default, followed by the object type, and finally, by the object property. For example, to specify a default value of 1.5 points for the line property LineWidth at the level of the current figure, use the statement

set(gcf,'DefaultLineLineWidth',1.5)

The string DefaultLineLineWidth identifies the property as a line property. To specify the figure color, use DefaultFigureColor. It is meaningful to specify a default figure color only on the root level.

set(0,'DefaultFigureColor','b')

Use get to determine what default values are currently set on any given object level:

get(gcf,'default')

returns all default values set on the current figure.

Setting Properties to the Default

Specifying a property value of 'default' sets the property to the first encountered default value defined for that property. For example, these statements result in a green surface EdgeColor:

set(0,'DefaultSurfaceEdgeColor','k')
h = surface(peaks);
set(gcf,'DefaultSurfaceEdgeColor','g')
set(h,'EdgeColor','default')

Because a default value for surface EdgeColor exists on the figure level, MATLAB encounters this value first and uses it instead of the default EdgeColor defined on the root.

Removing Default Values

Specifying a property value of 'remove' gets rid of user-defined default values. The statement

set(0,'DefaultSurfaceEdgeColor','remove')

removes the definition of the default Surface EdgeColor from the root.

Setting Properties to Factory-Defined Values

Specifying a property value of 'factory' sets the property to its factory-defined value. For example, these statements set the EdgeColor of surface h to black (its factory setting), regardless of what default values you have defined:

set(gcf,'DefaultSurfaceEdgeColor','g')
h = surface(peaks);
set(h,'EdgeColor','factory')

Reserved Words

Setting a property value to default, remove, or factory produces the effects described in the previous sections. To set a property to one of these words (e.g., a text or uicontrol String property set to the word default), you must precede the word with the backslash character:

h = uicontrol('Style','edit','String','\default');

Setting Default Line Styles

The plot function cycles through the colors defined by the axes ColorOrder property when displaying multiline plots. If you define more than one value for the axes LineStyleOrder property, MATLAB increments the line style after each cycle through the colors.

You can set default property values that cause the plot function to produce graphs using varying line styles, but not varying colors. This is useful when you are working on a monochrome display or printing on a black and white printer.

First Example

This example creates a figure with a white plot (axes) background color, and then sets default values for axes objects on the root level:

whitebg('w') %create a figure with a white color scheme
set(0,'DefaultAxesColorOrder',[0 0 0],...
      'DefaultAxesLineStyleOrder','-|--|:|-.')

Whenever you call plot,

Z = peaks; plot(1:49,Z(4:7,:))

it uses one color for all data plotted because the axes ColorOrder contains only one color, but it cycles through the line styles defined for LineStyleOrder.

Second Example

This example sets default values on more than one level in the hierarchy. These statements create two axes in one figure window, setting default values on the figure level and the axes level:

t = 0:pi/20:2*pi;
s = sin(t);
c = cos(t);
% Set default value for axes Color property
figh = figure('Position',[30 100 800 350],... 
              'DefaultAxesColor',[.8 .8 .8]); 

axh1 = subplot(1,2,1); grid on
% Set default value for line LineStyle property in first axes
set(axh1,'DefaultLineLineStyle','-.')
line('XData',t,'YData',s) 
line('XData',t,'YData',c) 
text('Position',[3 .4],'String','Sine')
text('Position',[2 -.3],'String','Cosine',...
     'HorizontalAlignment','right')

axh2 = subplot(1,2,2); grid on
% Set default value for text Rotation property in second axes
set(axh2,'DefaultTextRotation',90)
line('XData',t,'YData',s) 
line('XData',t,'YData',c) 
text('Position',[3 .4],'String','Sine')
text('Position',[2 -.3],'String','Cosine',...
     'HorizontalAlignment','right') 

Issuing the same line and text statements to each subplot region results in a different display, reflecting different default settings.

Because the default axes Color property is set on the figure level of the hierarchy, MATLAB creates both axes with the specified gray background color.

The axes on the left (subplot region 121) defines a dash-dot line style (-.) as the default, so each call to the line function uses dash-dot lines. The axes on the right does not define a default line style, so MATLAB uses solid lines (the factory setting for lines).

The axes on the right defines a default text Rotation of 90 degrees, which rotates all text by this amount. MATLAB obtains all other property values from their factory settings, which results in nonrotated text on the left.

To install default values whenever you run MATLAB, specify them in your startup.m file. MATLAB might install default values for some appearance properties when started by calling the colordef command.

Was this topic helpful?