<html lang="en"> <head> <title>Object Groups - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Advanced-Plotting.html#Advanced-Plotting" title="Advanced Plotting"> <link rel="prev" href="Application_002ddefined-Data.html#Application_002ddefined-Data" title="Application-defined Data"> <link rel="next" href="Graphics-Toolkits.html#Graphics-Toolkits" title="Graphics Toolkits"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Object-Groups"></a> <p> Next: <a rel="next" accesskey="n" href="Graphics-Toolkits.html#Graphics-Toolkits">Graphics Toolkits</a>, Previous: <a rel="previous" accesskey="p" href="Application_002ddefined-Data.html#Application_002ddefined-Data">Application-defined Data</a>, Up: <a rel="up" accesskey="u" href="Advanced-Plotting.html#Advanced-Plotting">Advanced Plotting</a> <hr> </div> <h4 class="subsection">15.4.6 Object Groups</h4> <p><a name="index-object-groups-1558"></a> 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 <code>hggroup</code> <!-- hggroup scripts/plot/hggroup.m --> <p><a name="doc_002dhggroup"></a> <div class="defun"> — Function File: <b>hggroup</b> ()<var><a name="index-hggroup-1559"></a></var><br> — Function File: <b>hggroup</b> (<var>h</var>)<var><a name="index-hggroup-1560"></a></var><br> — Function File: <b>hggroup</b> (<var><small class="dots">...</small>, property, value, <small class="dots">...</small></var>)<var><a name="index-hggroup-1561"></a></var><br> <blockquote><p>Create group object with parent <var>h</var>. If no parent is specified, the group is created in the current axes. Return the handle of the group object created. <p>Multiple property-value pairs may be specified for the group, but they must appear in pairs. </p></blockquote></div> <p>For example a simple use of a <code>hggroup</code> might be <pre class="example"> 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"); </pre> <p class="noindent">which groups the two plots into a single object and controls their visibility directly. The default properties of an <code>hggroup</code> are the same as the set of common properties for the other graphics objects. Additional properties can be added with the <code>addproperty</code> function. <!-- addproperty src/graphics.cc --> <p><a name="doc_002daddproperty"></a> <div class="defun"> — Built-in Function: <b>addproperty</b> (<var>name, h, type</var>)<var><a name="index-addproperty-1562"></a></var><br> — Built-in Function: <b>addproperty</b> (<var>name, h, type, arg, <small class="dots">...</small></var>)<var><a name="index-addproperty-1563"></a></var><br> <blockquote><p>Create a new property named <var>name</var> in graphics object <var>h</var>. <var>type</var> determines the type of the property to create. <var>args</var> usually contains the default value of the property, but additional arguments might be given, depending on the type of the property. <p>The supported property types are: <dl> <dt><code>string</code><dd>A string property. <var>arg</var> contains the default string value. <br><dt><code>any</code><dd>An un-typed property. This kind of property can hold any octave value. <var>args</var> contains the default value. <br><dt><code>radio</code><dd>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. <br><dt><code>boolean</code><dd>A boolean property. This property type is equivalent to a radio property with "on|off" as accepted values. <var>arg</var> contains the default property value. <br><dt><code>double</code><dd>A scalar double property. <var>arg</var> contains the default value. <br><dt><code>handle</code><dd>A handle property. This kind of property holds the handle of a graphics object. <var>arg</var> contains the default handle value. When no default value is given, the property is initialized to the empty matrix. <br><dt><code>data</code><dd>A data (matrix) property. <var>arg</var> contains the default data value. When no default value is given, the data is initialized to the empty matrix. <br><dt><code>color</code><dd>A color property. <var>arg</var> 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). </dl> <p><var>type</var> 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<small class="dots">...</small>). This allows to clone an existing property into the graphics object <var>h</var>. <p>Examples: <pre class="example"> addproperty ("my_property", gcf, "string", "a string value"); addproperty ("my_radio", gcf, "radio", "val_1|val_2|{val_3}"); addproperty ("my_style", gcf, "linelinestyle", "--"); </pre> </blockquote></div> <p>Once a property in added to an <code>hggroup</code>, 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 <code>addlistener</code> function is used to define a callback function that is executed when the property is altered. <!-- addlistener src/graphics.cc --> <p><a name="doc_002daddlistener"></a> <div class="defun"> — Built-in Function: <b>addlistener</b> (<var>h, prop, fcn</var>)<var><a name="index-addlistener-1564"></a></var><br> <blockquote><p>Register <var>fcn</var> as listener for the property <var>prop</var> of the graphics object <var>h</var>. Property listeners are executed (in order of registration) when the property is set. The new value is already available when the listeners are executed. <p><var>prop</var> must be a string naming a valid property in <var>h</var>. <p><var>fcn</var> can be a function handle, a string or a cell array whose first element is a function handle. If <var>fcn</var> 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 <var>fcn</var> is a string, it must be any valid octave expression. If <var>fcn</var> 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. <p>Example: <pre class="example"> function my_listener (h, dummy, p1) fprintf ("my_listener called with p1=%s\n", p1); endfunction addlistener (gcf, "position", {@my_listener, "my string"}) </pre> </blockquote></div> <!-- dellistener src/graphics.cc --> <p><a name="doc_002ddellistener"></a> <div class="defun"> — Built-in Function: <b>dellistener</b> (<var>h, prop, fcn</var>)<var><a name="index-dellistener-1565"></a></var><br> <blockquote><p>Remove the registration of <var>fcn</var> as a listener for the property <var>prop</var> of the graphics object <var>h</var>. The function <var>fcn</var> must be the same variable (not just the same value), as was passed to the original call to <code>addlistener</code>. <p>If <var>fcn</var> is not defined then all listener functions of <var>prop</var> are removed. <p>Example: <pre class="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); </pre> </blockquote></div> <p>An example of the use of these two functions might be <pre class="example"> 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 </pre> <p class="noindent">that adds a <code>linestyle</code> property to the <code>hggroup</code> and propagating any changes its value to the children of the group. The <code>linkprop</code> function can be used to simplify the above to be <pre class="example"> 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"); </pre> <!-- linkprop scripts/plot/linkprop.m --> <p><a name="doc_002dlinkprop"></a> <div class="defun"> — Function File: <var>hlink</var> = <b>linkprop</b> (<var>h, prop</var>)<var><a name="index-linkprop-1566"></a></var><br> <blockquote><p>Link graphics object properties, such that a change in one is propagated to the others. The properties to link are given as a string of cell string array by <var>prop</var> and the objects containing these properties by the handle array <var>h</var>. <p>An example of the use of linkprop is <pre class="example"> 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", "--"); </pre> </blockquote></div> <p>These capabilities are used in a number of basic graphics objects. The <code>hggroup</code> objects created by the functions of Octave contain one or more graphics object and are used to: <ul> <li>group together multiple graphics objects, <li>create linked properties between different graphics objects, and <li>to hide the nominal user data, from the actual data of the objects. </ul> <p class="noindent">For example the <code>stem</code> function creates a stem series where each <code>hggroup</code> of the stem series contains two line objects representing the body and head of the stem. The <code>ydata</code> property of the <code>hggroup</code> 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 <pre class="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]' </pre> <p class="noindent">shows the difference between the <code>xdata</code> of the <code>hggroup</code> of a stem series object and the underlying line. <p>The basic properties of such group objects is that they consist of one or more linked <code>hggroup</code>, 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. <p>In addition the members of the group can also be linked to other graphics objects through callback functions. For example the baseline of the <code>bar</code> or <code>stem</code> functions is a line object, whose length and position are automatically adjusted, based on changes to the corresponding hggroup elements. <ul class="menu"> <li><a accesskey="1" href="Data-Sources-in-Object-Groups.html#Data-Sources-in-Object-Groups">Data Sources in Object Groups</a> <li><a accesskey="2" href="Area-Series.html#Area-Series">Area Series</a> <li><a accesskey="3" href="Bar-Series.html#Bar-Series">Bar Series</a> <li><a accesskey="4" href="Contour-Groups.html#Contour-Groups">Contour Groups</a> <li><a accesskey="5" href="Error-Bar-Series.html#Error-Bar-Series">Error Bar Series</a> <li><a accesskey="6" href="Line-Series.html#Line-Series">Line Series</a> <li><a accesskey="7" href="Quiver-Group.html#Quiver-Group">Quiver Group</a> <li><a accesskey="8" href="Scatter-Group.html#Scatter-Group">Scatter Group</a> <li><a accesskey="9" href="Stair-Group.html#Stair-Group">Stair Group</a> <li><a href="Stem-Series.html#Stem-Series">Stem Series</a> <li><a href="Surface-Group.html#Surface-Group">Surface Group</a> </ul> </body></html>