object behavior with Octave...

Ben Abbott bpabbott at mac.com
Sat Feb 14 11:23:18 CST 2009


James,

I'm not as familiar with Octave's objects as are others. You'll likely  
get much better more (and I expect better) information if your reply- 
all so that thread stays on the list.

I haven't tried this, but I'd create a m-file function called  
create_ball.m and place all the contents below in it.

function ball = create_ball ()
   ball.bounce = @bounce;
   ball.add_volume = @add_volume;
   ball.set_color = @set_color;
endfunction
function ret = bounce (varargins)
... bounce code here ...
endfunction
function ret = add_volumne (varargins)
... add the volume here ...
endfunction
function ret = set_color (varargins)
... set the color here ...
endfunction

The problem I see is that you'll need to access these function as  
ball.bounce(ball)

Perhaps there is a way to associate a handle with a specific instance  
of a structure, but I don't know how.

Do I understand your intention?

Ben

On Feb 14, 2009, at 11:58 AM, James Moliere wrote:

> Ben,
> Thanks!  For the tip below.  I need to keep everything in functions  
> (or
> structures).
>
> I'd really like 1 .m file to hold multiple functions that a user can
> call.  Since Octave does not have the concept of a namespace or  
> package,
> it is possible to create LOTS of .m files but over time, it will be
> difficult manage.
>
> To give a more concrete example, I'd like to use a structure as an
> object such as Ball.  I could create functions like bounce(),
> addVolume(air), setColor(color), etc.  I would also like a .m file  
> named
> createBall.m that returns the structure and it's respective methods  
> for
> a ball.
>
> I'm looking at the docs in the 'GNU Octave Manual 3.3' and am not
> finding anything satisfactory.
>
>
> James
>
> On Sat, 2009-02-14 at 08:34 -0800, Ben Abbott wrote:
>> 	
>> On Feb 14, 2009, at 9:54 AM, James Moliere wrote:
>>
>>>
>>> looking at the URL
>>> http://asis.epfl.ch/SCI.MATH/octave-2.0.16/Octave-FAQ_3.html
>>>
>>> I don't see the capability below.  Can I assign functions to  
>>> variables
>>> in a structure and call them?
>>>
>>>
>>> # simple increment example
>>> function [ ret ] = tt (a)
>>>       x.a=a
>>>       function result = anon(x)
>>>               x.a = x.a+1
>>>               result = x
>>>       endfunction
>>>       x.increment = @anon
>>>       ret = x
>>> endfunction
>>>
>>> ... I'd like to do something like this
>>>
>>> k = tt(1)
>>> k.increment(k) % or k.increment() without passing in k
>>> ... I'd expect an answer of 2.
>>
>>
>> Your example can be entered at the command line as ...
>>
>> function result = anon(x)
>>        x.a = x.a+1;
>>        result = x;
>> endfunction
>> function [ ret ] = tt (a)
>>        x.a=a;
>>        x.increment = @anon;
>>        ret = x;
>> endfunction
>>
>> x.a = 0;
>>
>> anon(x)
>> ans =
>> {
>>   a =  1
>> }
>>
>> x = tt(1)
>> x =
>> {
>>   a =  1
>>   increment =
>>
>> anon
>> }
>>
>> x.increment(x)
>> ans =
>> {
>>   a =  2
>>   increment =
>>
>> anon
>> }
>>
>> If you try x.increment() it will produce an error, because that is  
>> the
>> same as calling anon().
>>
>> Ben
>



More information about the Help-octave mailing list