Default Property Values - MATLAB & Simulink (original) (raw)
Predefined Values for Properties
Nearly all graphics object properties have predefined values. Predefined values originate from two possible sources:
- Default values defined on an ancestor of the object
- Factory values defined on the root of the graphics object hierarchy
Users can create default values for an object property, which take precedence over the factory-defined values. Objects use default values when:
- Created in a hierarchy where an ancestor defines a default value
- Parented into a hierarchy where an ancestor defines a default value
Specify Default Values
Define a default property value as a string with these three parts:
"default" ObjectType PropertyName
- The word
default
- The object type (for example,
Line
) - The property name (for example,
LineWidth
)
A string that specified the default line LineWidth
would be:
Use this string to specify the default value. For example, to specify a default value of 2 points for the line LineWidth
property, use the statement:
set(groot,"defaultLineLineWidth",2)
You can also use the Name=Value
syntax and omit the quotes.
set(groot,defaultLineLineWidth=2)
The string defaultLineLineWidth
identifies the property as a line property. To specify the figure color, use defaultFigureColor
.
set(groot,defaultFigureColor="blue")
Where in Hierarchy to Define Default
In general, you should define a default value on the root level so that all subsequent plotting functions use those defaults. Specify the root in set
andget
statements using the groot function, which returns the handle to the root.
You can define default property values on three levels:
- Root — values apply to objects created in current MATLAB® session
- Figure — use for default values applied to children of the figure defining the defaults.
- Axes — use for default values applied only to children of the axes defining the defaults and only when using low-level functions (light, line, patch, rectangle, surface, text, and the low-level form ofimage).
For example, specify a default figure color only on the root level.
set(groot,defaultFigureColor="blue")
List Default Values
Use get to determine what default values are currently set on any given object level. This command returns all default values set in your current MATLAB session.
Set Properties to the Current 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 surfaceEdgeColor
:
set(groot,defaultSurfaceEdgeColor="black") 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 defaultEdgeColor
defined on the root.
Remove Default Values
Specifying a property value of "remove"
gets rid of user-defined default values. This command removes the definition of the default surfaceEdgeColor
from the root.
set(groot,defaultSurfaceEdgeColor="remove")
Set 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="green") h = surface(peaks); set(h,EdgeColor="factory")
List Factory-Defined Property Values
You can list factory values:
get(groot,"factory")
— List all factory-defined property values for all graphics objectsget(groot,"factory_`ObjectType`_")
— List all factory-defined property values for a specific objectget(groot,"factory_`ObjectTypePropertyName`_")
— List factory-defined value for the specified property.
Reserved Words
Setting a property value to default
, remove
, orfactory
produces the effects described in the previous sections. To set a property to one of these words (for example, a text String
property set to the word default
), precede the word with the backslash character:
h = text(String="\default");
Effect of Figure Themes on Default Values
Since R2025a
Many default color property values and a few default transparency property values change depending on the theme. For example, the default Text
object color in the light theme is a dark shade of gray.
f = figure(Theme="light"); get(f,"DefaultTextColor")
ans =
0.1294 0.1294 0.1294
If you change the theme of the figure, the default color is a much lighter shade of gray.
f.Theme = "dark"; get(f,"DefaultTextColor")
ans =
0.8510 0.8510 0.8510
However, the factory default values depend on the default figure theme regardless of the current figure's theme. For example, if the default theme of the figure is"light"
, the factory default text color is dark.
get(f,"FactoryTextColor")
ans =
0.1294 0.1294 0.1294
Note
You cannot set or get the default value of the figure theme using the default syntaxes. Instead, use the MATLAB Settings window. On the Home tab of the MATLAB desktop, in the Environment section, click Settings and select MATLAB > Appearance. Then select an item from the Figure Theme menu.
If you specify the default value of a property, the default value persists even if you change the theme of the figure. For example, set the default Text
object color to [1 0 0]
(red).
set(f,"DefaultTextColor",[1 0 0]) get(f,"DefaultTextColor")
Change the theme of the figure and get the default text color again. The value is still red.
f.Theme = "light"; get(f,"DefaultTextColor")