34.2 Class Methods

There are a number of basic class methods that can (and should) be defined to allow the contents of the classes to be queried and set. The most basic of these is the disp method. The disp method is used by Octave whenever a class should be displayed on the screen. Usually this is the result of an Octave expression that doesn’t end with a semicolon. If this method is not defined, then Octave won’t print anything when displaying the contents of a class which can be confusing.

An example of a disp method for the polynomial class might be

function disp (p)

  a = p.poly;
  first = true;
  for i = 1 : length (a);
    if (a(i) != 0)
      if (first)
        first = false;
      elseif (a(i) > 0 || isnan (a(i)))
        printf (" +");
      endif
      if (a(i) < 0)
        printf (" -");
      endif
      if (i == 1)
        printf (" %.5g", abs (a(i)));
      elseif (abs (a(i)) != 1)
        printf (" %.5g *", abs (a(i)));
      endif
      if (i > 1)
        printf (" X");
      endif
      if (i > 2)
        printf (" ^ %d", i - 1);
      endif
    endif
  endfor

  if (first)
    printf (" 0");
  endif
  printf ("\n");

endfunction

To be consistent with the Octave graphic handle classes, a class should also define the get and set methods. The get method accepts one or two arguments. The first argument is an object of the appropriate class. If no second argument is given then the method should return a structure with all the properties of the class. If the optional second argument is given it should be a property name and the specified property should be retrieved.

function val = get (p, prop)

  if (nargin < 1 || nargin > 2)
    print_usage ();
  endif

  if (nargin == 1)
    val.poly = p.poly;
  else
    if (! ischar (prop))
      error ("@polynomial/get: PROPERTY must be a string");
    endif

    switch (prop)
      case "poly"
        val = p.poly;
      otherwise
        error ('@polynomial/get: invalid PROPERTY "%s"', prop);
    endswitch
  endif

endfunction

Similarly, the first argument to the set method should be an object and any additional arguments should be property/value pairs.

function pout = set (p, varargin)

  if (numel (varargin) < 2 || rem (numel (varargin), 2) != 0)
    error ("@polynomial/set: expecting PROPERTY/VALUE pairs");
  endif

  pout = p;
  while (numel (varargin) > 1)
    prop = varargin{1};
    val  = varargin{2};
    varargin(1:2) = [];
    if (! ischar (prop) || ! strcmp (prop, "poly"))
      error ("@polynomial/set: invalid PROPERTY for polynomial class");
    elseif (! (isreal (val) && isvector (val)))
      error ("@polynomial/set: VALUE must be a real vector");
    endif

    pout.poly = val(:).';  # force row vector
  endwhile

endfunction

Note that Octave does not implement pass by reference; Therefore, to modify an object requires an assignment statement using the return value from the set method.

p = set (p, "poly", [1, 0, 0, 0, 1]);

The set method makes use of the subsasgn method of the class, and therefore this method must also be defined. The subsasgn method is discussed more thoroughly in the next section (see Indexing Objects).

Finally, user classes can be considered to be a special type of a structure, and they can be saved to a file in the same manner as a structure. For example:

p = polynomial ([1, 0, 1]);
save userclass.mat p
clear p
load userclass.mat

All of the file formats supported by save and load are supported. In certain circumstances a user class might contain a field that it doesn’t make sense to save, or a field that needs to be initialized before it is saved. This can be done with the saveobj method of the class.

: b = saveobj (a)

Method of a class to manipulate an object prior to saving it to a file.

The function saveobj is called when the object a is saved using the save function. An example of the use of saveobj 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:

function b = saveobj (a)
  b = a;
  if (isempty (b.field))
     b.field = initfield (b);
  endif
endfunction

See also: loadobj, class.

saveobj is called just prior to saving the class to a file. Similarly, the loadobj 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.

: b = loadobj (a)

Method of a class to manipulate an object after loading it from a file.

The function loadobj is called when the object a is loaded using the load function. An example of the use of saveobj might be to add fields to an object that don’t make sense to be saved. For example:

function b = loadobj (a)
  b = a;
  b.addmissingfield = addfield (b);
endfunction

See also: saveobj, class.

© 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/Class-Methods.html