Main Content

Define an Object Creation Callback

This example shows how to define an object creation callback.

Define an object creation callback that specifies values for the LineWidth and Marker properties of line objects.

function lineCreate(src,~)
   src.LineWidth = 2;
   src.Marker = 'o';
end

Assign this function as the default line creation callback using the line CreateFcn property:

set(groot,'defaultLineCreateFcn',@lineCreate)

The groot function specifies the root of the graphics object hierarchy. Therefore, all lines created in any given MATLAB® session acquire this callback. All plotting functions that create lines use these defaults.

An object’s creation callback executes directly after MATLAB creates the object and sets all its property values. Therefore, the creation callback can override property name/value pairs specified in a plotting function. For example:

set(groot,'defaultLineCreateFcn',@lineCreate)
h = plot(1:10,'LineWidth',.5,'Marker','none')

The creation callback executes after the plot function execution is complete. The LineWidth and Marker property values of the resulting line are those values specified in the creation callback:

h = 

  Line  with properties:

              Color: [0 0 1]
          LineStyle: '-'
          LineWidth: 2
             Marker: 'o'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [1 2 3 4 5 6 7 8 9 10]
              YData: [1 2 3 4 5 6 7 8 9 10]
              ZData: []

Related Information

For information about defining callback functions, see Create Callbacks for Graphics Objects