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