help-octave
[Top][All Lists]
Advanced

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

Re: Automatically Combining Structures


From: Bill Denney
Subject: Re: Automatically Combining Structures
Date: Wed, 26 Apr 2006 10:15:51 -0400 (EDT)

Bill asked
  Is there a way to automatically combine structures?  What I'm wanting to
  do is something like:

On Wed, 26 Apr 2006, Przemek Klosowski wrote:

but it doesn't substitute the _value_ of n, but rather
just sets s.n, so we need eval to the rescue:

function s=combine(varargin)
for i=1:nargin
  a=varargin{i}
  for [v,n]=a
    eval(strcat("s.",n,"=v"))
  endfor
endfor
endfunction

So, is there a simpler way to write it?

You actually don't need eval here.  You can use dynamic field names like:

function s=combine(varargin)
for i=1:nargin
  a=varargin{i}
  for [v,n]=a
    s.(n)=v;
  endfor
endfor
endfunction

I didn't know about the ability to iterate over a structure like that before this thread though.

Bill

--
Q: "But Herr Mozart, you were writing symphonies when you
were 8 years old."
A: "But I never asked anybody how."
  -- /usr/bin/fortune



reply via email to

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