Main Content

Create Object Groups

Create an object group by parenting objects to a group or transform object. For example, call hggroup to create a group object and save its handle. Assign this group object as the parent of subsequently created objects:

hg = hggroup;
plot(rand(5),'Parent',hg)
text(3,0.5,'Random lines','Parent',hg)

Setting the visibility of the group to off makes the line and text objects it contains invisible.

hg.Visible = 'off';

You can add objects to a group selectively. For example, the following call to the bar function returns the handles to five separate bar objects:

hb = bar(randn(5))
hb = 

  1x5 Bar array:

    Bar    Bar    Bar    Bar    Bar

Parent the third, fourth, and fifth bar object to the group:

hg = hggroup;
set(hb(3:5),'Parent',hg) 

Group objects can be the parent of any number of axes children, including other group objects. For examples, see Rotate About an Arbitrary Axis and Nest Transforms for Complex Movements.

Parent Specification

Plotting functions clear the axes before generating their graph. However, if you assign a group or transform as the Parent in the plotting function, the group or transform object is not cleared.

For example:

hg = hggroup;
hb = bar(randn(5));
set(hb,'Parent',hg)
Error using matlab.graphics.chart.primitive.Bar/set
Cannot set property to a deleted object

The bar function cleared the axes. However, if you set the Parent property as a name/value pair in the bar function arguments, the bar function does not delete the group:

hg = hggroup;
hb = bar(randn(5),'Parent',hg);

Visible and Selected Properties of Group Children

Setting the Visible property of a group or transform object controls whether all the objects in the group are visible or not visible. However, changing the state of the Visible property for the group object does not change the state of this property for the individual objects. The values of the Visible property for the individual objects are preserved.

For example, if the Visible property of the group is set to off and subsequently set to on, only the objects that were originally visible are displayed.

The same behavior applies to the Selected and SelectionHighlight properties. The children of the group or transform object show the state of the containing object properties without actually changing their own property values.