octave-maintainers
[Top][All Lists]
Advanced

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

Re: getstruct, setstruct, etc.


From: Andy Adler
Subject: Re: getstruct, setstruct, etc.
Date: Thu, 22 Jul 2004 16:03:08 -0400 (EDT)

> On Jul 21, 2004, at 4:21 PM, Andy Adler wrote:
>
> > QUESTION:
> > Is anyone feel strongly about changing the octave-forge functions
> > to be closer to Matlab?
>
> I would prefer it, but getfield/setfield are used elsewhere in
> octave-forge,
> so make sure they don't break.
>
> You should probably keep the old versions around but rename them
> to make it easier for those who depend on the existing  behaviour in
> their private scripts.

The only parts of octave-forge that depend on this behaviour are:

./main/miscellaneous/read_options.m
./main/optim/minimize.m
./main/struct/test_struct.m:     # this is obvious
./main/vrml/                     # many functions

I can modify this. Any ideas on what to rename them to?

Here is my proposed fix. They're written as *.m files, but that
should be OK since this syntax is deprecated anyway.

Andy

function v= getfield( obj, varargin )
% GETFIELD: access field members in a structure
% example: given  ss(1,1).fd(1).b=5;
%          getfield(ss,{1,1},'fd',{1},'b') => 5

   field= field_access( varargin{1:nargin-1} );
   v= eval(field);
endfunction

function obj= setfield( obj, varargin )
% SETFIELD: access field members in a structure
% example: ss= setfield(ss,{1,1},'fd',{1},'b', 6);
%  gives  ss(1,1).fd(1).b == 6

   field= field_access( varargin{1:nargin-2} )
   val= varargin{ nargin-1 };
   eval([field ,'=val;']);
endfunction

function str= field_access( varargin )
   str= 'obj';
   for i=1:nargin
      v= varargin{i};
      if iscell( v )
          sep= '(';
          for j=1:length(v)
              str= [str, sep, num2str(v{j}) ];
              sep= ',';
          end
          str= [str, ')'];
      else
          str= [str, '.', v];
      end
   end
endfunction



reply via email to

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