| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 
 | <html lang="en">
<head>
<title>Manipulating Classes - 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="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">34.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.
<!-- display scripts/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-2992"></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
<!-- saveobj scripts/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-2993"></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.
<!-- loadobj scripts/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-2994"></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>
 |