<html lang="en"> <head> <title>Manipulating Classes - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Object-Oriented-Programming.html#Object-Oriented-Programming" title="Object Oriented Programming"> <link rel="prev" href="Creating-a-Class.html#Creating-a-Class" title="Creating a Class"> <link rel="next" href="Indexing-Objects.html#Indexing-Objects" title="Indexing Objects"> <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="Manipulating-Classes"></a> <p> Next: <a rel="next" accesskey="n" href="Indexing-Objects.html#Indexing-Objects">Indexing Objects</a>, Previous: <a rel="previous" accesskey="p" href="Creating-a-Class.html#Creating-a-Class">Creating a Class</a>, Up: <a rel="up" accesskey="u" href="Object-Oriented-Programming.html#Object-Oriented-Programming">Object Oriented Programming</a> <hr> </div> <h3 class="section">33.2 Manipulating Classes</h3> <p>There are a number of basic classes methods that can be defined to allow the contents of the classes to be queried and set. The most basic of these is the <code>display</code> method. The <code>display</code> method is used by Octave when displaying a class on the screen, due to an expression that is not terminated with a semicolon. If this method is not defined, then Octave will printed nothing when displaying the contents of a class. <!-- ./general/display.m --> <p><a name="doc_002ddisplay"></a> <div class="defun"> — Function File: <b>display</b> (<var>a</var>)<var><a name="index-display-2258"></a></var><br> <blockquote><p>Display the contents of an object. If <var>a</var> is an object of the class "myclass", then <code>display</code> is called in a case like <pre class="example"> myclass (...) </pre> <p class="noindent">where Octave is required to display the contents of a variable of the type "myclass". <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dclass.html#doc_002dclass">class</a>, <a href="doc_002dsubsref.html#doc_002dsubsref">subsref</a>, <a href="doc_002dsubsasgn.html#doc_002dsubsasgn">subsasgn</a>. </p></blockquote></div> <p class="noindent">An example of a display method for the polynomial class might be <pre class="example"><pre class="verbatim"> function display (p) a = p.poly; first = true; fprintf("%s =", inputname(1)); for i = 1 : length (a); if (a(i) != 0) if (first) first = false; elseif (a(i) > 0) fprintf (" +"); endif if (a(i) < 0) fprintf (" -"); endif if (i == 1) fprintf (" %g", abs (a(i))); elseif (abs(a(i)) != 1) fprintf (" %g *", abs (a(i))); endif if (i > 1) fprintf (" X"); endif if (i > 2) fprintf (" ^ %d", i - 1); endif endif endfor if (first) fprintf(" 0"); endif fprintf("\n"); endfunction </pre> </pre> <p class="noindent">Note that in the display method, it makes sense to start the method with the line <code>fprintf("%s =", inputname(1))</code> to be consistent with the rest of Octave and print the variable name to be displayed when displaying the class. <p>To be consistent with the Octave graphic handle classes, a class should also define the <code>get</code> and <code>set</code> methods. The <code>get</code> method should accept one or two arguments, and given one argument of the appropriate class it should return a structure with all of the properties of the class. For example <pre class="example"><pre class="verbatim"> function s = get (p, f) if (nargin == 1) s.poly = p.poly; elseif (nargin == 2) if (ischar (f)) switch (f) case "poly" s = p.poly; otherwise error ("get: invalid property %s", f); endswitch else error ("get: expecting the property to be a string"); endif else print_usage (); endif endfunction </pre> </pre> <p class="noindent">Similarly, the <code>set</code> method should taken as its first argument an object to modify, and then take property/value pairs to be modified. <pre class="example"><pre class="verbatim"> function s = set (p, varargin) s = p; if (length (varargin) < 2 || rem (length (varargin), 2) != 0) error ("set: expecting property/value pairs"); endif while (length (varargin) > 1) prop = varargin{1}; val = varargin{2}; varargin(1:2) = []; if (ischar (prop) && strcmp (prop, "poly")) if (isvector (val) && isreal (val)) s.poly = val(:).'; else error ("set: expecting the value to be a real vector"); endif else error ("set: invalid property of polynomial class"); endif endwhile endfunction </pre> </pre> <p class="noindent">Note that as Octave does not implement pass by reference, than the modified object is the return value of the <code>set</code> method and it must be called like <pre class="example"> p = set (p, "a", [1, 0, 0, 0, 1]); </pre> <p class="noindent">Also the <code>set</code> method makes use of the <code>subsasgn</code> method of the class, and this method must be defined. The <code>subsasgn</code> method is discussed in the next section. <p>Finally, user classes can be considered as a special type of a structure, and so they can be saved to a file in the same manner as a structure. For example <pre class="example"> p = polynomial ([1, 0, 1]); save userclass.mat p clear p load userclass.mat </pre> <p class="noindent">All of the file formats supported by <code>save</code> and <code>load</code> are supported. In certain circumstances, a user class might either contain a field that it makes no sense to save or a field that needs to be initialized before it is saved. This can be done with the <code>saveobj</code> method of the class <!-- ./general/saveobj.m --> <p><a name="doc_002dsaveobj"></a> <div class="defun"> — Function File: <var>b</var> = <b>saveobj</b> (<var>a</var>)<var><a name="index-saveobj-2259"></a></var><br> <blockquote><p>Method of a class to manipulate an object prior to saving it to a file. The function <code>saveobj</code> is called when the object <var>a</var> is saved using the <code>save</code> function. An example of the use of <code>saveobj</code> might be to remove fields of the object that don't make sense to be saved or it might be used to ensure that certain fields of the object are initialized before the object is saved. For example <pre class="example"> function b = saveobj (a) b = a; if (isempty (b.field)) b.field = initfield(b); endif endfunction </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dloadobj.html#doc_002dloadobj">loadobj</a>, <a href="doc_002dclass.html#doc_002dclass">class</a>. </p></blockquote></div> <p class="noindent"><code>saveobj</code> is called just prior to saving the class to a file. Likely, the <code>loadobj</code> method is called just after a class is loaded from a file, and can be used to ensure that any removed fields are reinserted into the user object. <!-- ./general/loadobj.m --> <p><a name="doc_002dloadobj"></a> <div class="defun"> — Function File: <var>b</var> = <b>loadobj</b> (<var>a</var>)<var><a name="index-loadobj-2260"></a></var><br> <blockquote><p>Method of a class to manipulate an object after loading it from a file. The function <code>loadobj</code> is called when the object <var>a</var> is loaded using the <code>load</code> function. An example of the use of <code>saveobj</code> might be to add fields to an object that don't make sense to be saved. For example <pre class="example"> function b = loadobj (a) b = a; b.addmissingfield = addfield (b); endfunction </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsaveobj.html#doc_002dsaveobj">saveobj</a>, <a href="doc_002dclass.html#doc_002dclass">class</a>. </p></blockquote></div> </body></html>