help-octave
[Top][All Lists]
Advanced

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

Re: class object methods and 'eval' function


From: Ben Abbott
Subject: Re: class object methods and 'eval' function
Date: Thu, 15 Sep 2011 08:02:02 -0400

On Sep 15, 2011, at 7:42 AM, pantxo diribarne wrote:

> Hello list,
> 
> I am currently writing a class using octave @folder framework. 
> I need to use eval to access  the fields of my object in the methods 
> @myclass/get.m and @myclass/set.m but octave returns the same error as if I 
> was trying to access the object fields from outside a method. 
> 
> In get.m :
> function varargout = get(p, arg)
>   out = p.boardindex
>   eval (["out = p." arg ";"]);
>   varargout{ii} = out;
> endfunction
> 
> Calling get with  " get (obj, 'boardindex')", where obj is a 'myclass' 
> object, I obtain :
> 
> out =  2
> error: invalid index for class
> ...
> 
> Is there a way to have eval function know that it is evaluated inside a 
> method, or should I find the way not to use eval at all?
> 
> Pantxo

The line below will throw an error.

        eval (["out = p." arg ";"]);

You've forgotten to include the commas.

        eval (["out = p.", arg, ";"]);

However, a better approach is ...

        function varargout = get (p, arg)
          varargout{ii} = p.(arg);
        endfunction

... but you still haven't defined "ii". Did you want ...

        function varargout = get (p, arg)
          varargout = {p.(arg)};
        endfunction

... or did you intend to support multiple arg's?

Ben



reply via email to

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