help-octave
[Top][All Lists]
Advanced

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

testing class members


From: Massimo Manghi
Subject: testing class members
Date: Wed, 26 Sep 2012 18:46:15 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.6esrpre) Gecko/20120817 Icedove/10.0.6

Hi

I've been searching for an answer to this apparently simple problems, but I couldn't find it

The function 'isfield' and 'fieldnames' can be called to get info about a structure

s.a=1;
s.b=2;
fildnames(s)
ans =
{
  [1,1] = a
  [2,1] = b
}
isfield(s,'a')
ans = 1
isfield(s,'c')
ans = 0

function 'fieldnames' does work the same for a class:

e.g.

@simple/simple.m:

function robj = simple(a1)
    if (strcmp(class(a1),'simple'))
        robj = a1;
    else
        robj.a = a1;
        robj.b = a1+1;

        robj = class(robj,'simple');
    endif
endfunction

octave:1> s = simple(1)
s = <class simple>
octave:2> fieldnames(s)
ans =
{
  [1,1] = a
  [2,1] = b
}

Still function 'isfield' seems not to know how to handle a class object

octave:3> isfield(s,'a')
ans = 0

I wrote a simple function that emulates isfield for a class, but I'm wary I'm writing code that maybe relies on some unsupported feature or I could even missing the right way to approach the problem.

  -- Massimo


reply via email to

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