15.4.6 Object Groups

A number of Octave high level plot functions return groups of other graphics objects or they return graphics objects that have their properties linked in such a way that changes to one of the properties results in changes in the others. A graphic object that groups other objects is an hggroup

: hggroup ()
: hggroup (hax)
: hggroup (…, property, value, …)
: h = hggroup (…)

Create handle graphics group object with axes parent hax.

If no parent is specified, the group is created in the current axes.

Multiple property/value pairs may be specified for the hggroup, but they must appear in pairs. The full list of properties is documented at Axes Properties.

The optional return value h is a graphics handle to the created hggroup object.

Programming Note: An hggroup is a way to group base graphics objects such as line objects or patch objects into a single unit which can react appropriately. For example, the individual lines of a contour plot are collected into a single hggroup so that they can be made visible/invisible with a single command, set (hg_handle, "visible", "off").

See also: addproperty, addlistener.

For example a simple use of a hggroup might be

x = 0:0.1:10;
hg = hggroup ();
plot (x, sin (x), "color", [1, 0, 0], "parent", hg);
hold on
plot (x, cos (x), "color", [0, 1, 0], "parent", hg);
set (hg, "visible", "off");

which groups the two plots into a single object and controls their visibility directly. The default properties of an hggroup are the same as the set of common properties for the other graphics objects. Additional properties can be added with the addproperty function.

: addproperty (name, h, type)
: addproperty (name, h, type, arg, …)

Create a new property named name in graphics object h.

type determines the type of the property to create. args usually contains the default value of the property, but additional arguments might be given, depending on the type of the property.

The supported property types are:

string

A string property. arg contains the default string value.

any

An un-typed property. This kind of property can hold any octave value. args contains the default value.

radio

A string property with a limited set of accepted values. The first argument must be a string with all accepted values separated by a vertical bar (’|’). The default value can be marked by enclosing it with a ’{’ ’}’ pair. The default value may also be given as an optional second string argument.

boolean

A boolean property. This property type is equivalent to a radio property with "on|off" as accepted values. arg contains the default property value.

double

A scalar double property. arg contains the default value.

handle

A handle property. This kind of property holds the handle of a graphics object. arg contains the default handle value. When no default value is given, the property is initialized to the empty matrix.

data

A data (matrix) property. arg contains the default data value. When no default value is given, the data is initialized to the empty matrix.

color

A color property. arg contains the default color value. When no default color is given, the property is set to black. An optional second string argument may be given to specify an additional set of accepted string values (like a radio property).

type may also be the concatenation of a core object type and a valid property name for that object type. The property created then has the same characteristics as the referenced property (type, possible values, hidden state…). This allows one to clone an existing property into the graphics object h.

Examples:

addproperty ("my_property", gcf, "string", "a string value");
addproperty ("my_radio", gcf, "radio", "val_1|val_2|{val_3}");
addproperty ("my_style", gcf, "linelinestyle", "--");

See also: addlistener, hggroup.

Once a property in added to an hggroup, it is not linked to any other property of either the children of the group, or any other graphics object. Add so to control the way in which this newly added property is used, the addlistener function is used to define a callback function that is executed when the property is altered.

: addlistener (h, prop, fcn)

Register fcn as listener for the property prop of the graphics object h.

Property listeners are executed (in order of registration) when the property is set. The new value is already available when the listeners are executed.

prop must be a string naming a valid property in h.

fcn can be a function handle, a string or a cell array whose first element is a function handle. If fcn is a function handle, the corresponding function should accept at least 2 arguments, that will be set to the object handle and the empty matrix respectively. If fcn is a string, it must be any valid octave expression. If fcn is a cell array, the first element must be a function handle with the same signature as described above. The next elements of the cell array are passed as additional arguments to the function.

Example:

function my_listener (h, dummy, p1)
  fprintf ("my_listener called with p1=%s\n", p1);
endfunction

addlistener (gcf, "position", {@my_listener, "my string"})

See also: dellistener, addproperty, hggroup.

: dellistener (h, prop, fcn)

Remove the registration of fcn as a listener for the property prop of the graphics object h.

The function fcn must be the same variable (not just the same value), as was passed to the original call to addlistener.

If fcn is not defined then all listener functions of prop are removed.

Example:

function my_listener (h, dummy, p1)
  fprintf ("my_listener called with p1=%s\n", p1);
endfunction

c = {@my_listener, "my string"};
addlistener (gcf, "position", c);
dellistener (gcf, "position", c);

See also: addlistener.

An example of the use of these two functions might be

x = 0:0.1:10;
hg = hggroup ();
h = plot (x, sin (x), "color", [1, 0, 0], "parent", hg);
addproperty ("linestyle", hg, "linelinestyle", get (h, "linestyle"));
addlistener (hg, "linestyle", @update_props);
hold on
plot (x, cos (x), "color", [0, 1, 0], "parent", hg);

function update_props (h, d)
  set (get (h, "children"), "linestyle", get (h, "linestyle"));
endfunction

that adds a linestyle property to the hggroup and propagating any changes its value to the children of the group. The linkprop function can be used to simplify the above to be

x = 0:0.1:10;
hg = hggroup ();
h1 = plot (x, sin (x), "color", [1, 0, 0], "parent", hg);
addproperty ("linestyle", hg, "linelinestyle", get (h, "linestyle"));
hold on
h2 = plot (x, cos (x), "color", [0, 1, 0], "parent", hg);
hlink = linkprop ([hg, h1, h2], "color");
: hlink = linkprop (h, "prop")
: hlink = linkprop (h, {"prop1", "prop2", …})

Link graphic object properties, such that a change in one is propagated to the others.

The input h is a vector of graphic handles to link.

prop may be a string when linking a single property, or a cell array of strings for multiple properties. During the linking process all properties in prop will initially be set to the values that exist on the first object in the list h.

The function returns hlink which is a special object describing the link. As long as the reference hlink exists, the link between graphic objects will be active. This means that hlink must be preserved in a workspace variable, a global variable, or otherwise stored using a function such as setappdata or guidata. To unlink properties, execute clear hlink.

An example of the use of linkprop is

x = 0:0.1:10;
subplot (1,2,1);
h1 = plot (x, sin (x));
subplot (1,2,2);
h2 = plot (x, cos (x));
hlink = linkprop ([h1, h2], {"color","linestyle"});
set (h1, "color", "green");
set (h2, "linestyle", "--");

See also: linkaxes, addlistener.

: linkaxes (hax)
: linkaxes (hax, optstr)

Link the axis limits of 2-D plots such that a change in one is propagated to the others.

The axes handles to be linked are passed as the first argument hax.

The optional second argument is a string which defines which axis limits will be linked. The possible values for optstr are:

"x"

Link x-axes

"y"

Link y-axes

"xy" (default)

Link both axes

"off"

Turn off linking

If unspecified the default is to link both X and Y axes.

When linking, the limits from the first axes in hax are applied to the other axes in the list. Subsequent changes to any one of the axes will be propagated to the others.

See also: linkprop, addproperty.

These capabilities are used in a number of basic graphics objects. The hggroup objects created by the functions of Octave contain one or more graphics object and are used to:

  • group together multiple graphics objects,
  • create linked properties between different graphics objects, and
  • to hide the nominal user data, from the actual data of the objects.

For example the stem function creates a stem series where each hggroup of the stem series contains two line objects representing the body and head of the stem. The ydata property of the hggroup of the stem series represents the head of the stem, whereas the body of the stem is between the baseline and this value. For example

h = stem (1:4)
get (h, "xdata")
⇒ [  1   2   3   4]'
get (get (h, "children")(1), "xdata")
⇒ [  1   1 NaN   2   2 NaN   3   3 NaN   4   4 NaN]'

shows the difference between the xdata of the hggroup of a stem series object and the underlying line.

The basic properties of such group objects is that they consist of one or more linked hggroup, and that changes in certain properties of these groups are propagated to other members of the group. Whereas, certain properties of the members of the group only apply to the current member.

In addition the members of the group can also be linked to other graphics objects through callback functions. For example the baseline of the bar or stem functions is a line object, whose length and position are automatically adjusted, based on changes to the corresponding hggroup elements.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Object-Groups.html