help-octave
[Top][All Lists]

octave and oo

 From: John W. Eaton Subject: octave and oo Date: Sun, 14 Nov 2010 02:07:17 -0500

On 13-Nov-2010, Ernst Reissner wrote:

| Hi all,
| at the moment i work through a tutorial with oo in Matlab
| http://www.mathworks.com/help/techdoc/matlab_oop/f3-28024.html
| and octave and try to reproduce the example there:
| It is about defining a class DocPolynom of polynomials.
| The constructor seems to work well,
|
|
| function obj = DocPolynom(c)
|    % Construct a DocPolynom object using the coefficients supplied
|    if isa(c,'DocPolynom')
|       obj.coef = c.coef;
|    else
|       obj.coef = c(:).';
|    end
| end
|
|
|
| but although it is do easy, the double-method does not:
|
|
| function c = double(obj)
|    % DocPolynom/Double Converter
|    c = obj.coef;
| end
|
|
|
|
| What i obtain is
|
|  p = DocPolynom([1 0 -2 -5])
| p =
| {
|   coef =
|
|      1   0  -2  -5
|
| }
|
| octave:16> c = double(p)
| error: struct type invalid as index value
|
| I am sure that my m-files are evaluated like char.m defined below,
| because it produces failures:
|
| octave:16> p = DocPolynom([1 0 -2 -5]);
| octave:17> char(p)
| error: scalar cannot be indexed with .
| error: called from:
| error:   /home/ernst/Software/resource/octave/m/@DocPolynom/double.m at line
3,
| column 3
| error: evaluating argument list element number 1
| error: evaluating argument list element number 1
| error: evaluating argument list element number 1
| error: evaluating argument list element number 1
| error:   /usr/share/octave/3.2.4/m/general/int2str.m at line 105, column 5
| error: evaluating argument list element number 1
| error: evaluating argument list element number 1
| error:   /usr/share/octave/3.2.4/m/general/int2str.m at line 69, column 11
| error: evaluating argument list element number 1
| error:   /home/ernst/Software/resource/octave/m/@DocPolynom/char.m at line 36,
| column 23
|
|
| what is wrong here??

Are you sure this code works in Matlab?  If so, in what version?

The problem is that your DocPolynom constructor is returning a struct,
not a class object.  Try changing the constructor to be

function retval = DocPolynom(c)
% Construct a DocPolynom object using the coefficients supplied
if isa(c,'DocPolynom')
obj.coef = c.coef;
else
obj.coef = c(:).';
end
retval = class (obj, 'DocPolynom');
end

and it should work.

jwe