help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling a method of an object?


From: Joanna Rutkowska
Subject: Re: Calling a method of an object?
Date: Thu, 18 Oct 2012 22:08:32 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120911 Thunderbird/15.0.1

On 10/18/12 21:36, Robert T. Short wrote:
On 10/18/2012 12:15 PM, Joanna Rutkowska wrote:
On 10/18/12 21:00, Hoeltgen Laurent wrote:


2012/10/18 Joanna Rutkowska <address@hidden
<mailto:address@hidden>>

    Hello, I've created a simple class:

    function m = motor (name, Kv, Rm, I0)
    m.name <http://m.name> = name;
             m.Kv = Kv;
             m.Rm = Rm;
             m.I0 = I0;
             m = class (m, "motor");
    end

    ...and one simple method:

    function power = power_out (motor, u, i)
             power = (u - i*motor.Rm) * (i - motor.I0);
    end

    ... and I created an object of the class motor:

    octave:1> m1 = motor ("Sample motor", 400, 0.05, 1);

    ... but I cannot call the power_out method using the traditional "OO
    way":

    octave:2> m1.power_out (1,1);
    error: invalid index for class

    ... only this form seems to work:

    octave:2> power_out (m1, 1, 1);

    Is it how this is supposed to be in Octave? The object.method()
    calling convention is much more intuitive and more readable than
    method(object) IMHO. Also, it clearly allows to avoid name conflicts
    if I got some global function or another method with the same
name...

    Thanks,
    joanna.
    _________________________________________________
    Help-octave mailing list
    address@hidden <mailto:address@hidden>
    https://mailman.cae.wisc.edu/__listinfo/help-octave
    <https://mailman.cae.wisc.edu/listinfo/help-octave>



Hi,

according to
http://www.gnu.org/software/octave/doc/interpreter/Creating-a-Class.html#Creating-a-Class,

all class definitions must be inside folders starting with an @ symbol.
Did you do that?

Yes, I did:

address@hidden @motor]$ pwd
/rw/home/user/multirotor_tools/@motor
address@hidden @motor]$ ll
total 16
-rw-rw-r-- 1 user user 142 Oct 18 18:45 display.m
-rw-rw-r-- 1 user user 119 Oct 18 18:51 get.m
-rw-rw-r-- 1 user user 181 Oct 18 18:45 motor.m
-rw-rw-r-- 1 user user 171 Oct 18 21:14 power_out.m

joanna.

_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave


It may be clunky, but it is MATLAB syntax.  Since octave attempts to be
MATLAB compatible we are stuck with it.

So you have to use the documented syntax:

m = motor(...)
p = power(m, ...)


And what happens if there is also a global function with the same name? Or another method (of a different class) with the same name?

Thanks,
joanna.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]