[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Automatically Combining Structures
From: |
Przemek Klosowski |
Subject: |
Re: Automatically Combining Structures |
Date: |
Wed, 26 Apr 2006 09:46:58 -0400 (EDT) |
Bill asked
Is there a way to automatically combine structures? What I'm wanting to
do is something like:
a.c = 1;
a.d = 2;
b.e = 3;
b.f = 4;
g = combine(a, b)
g =
{
c = 1
d = 2
e = 3
f = 4
}
I thought something like this would work:
function s=combine(varargin)
for a = varargin{:}
for [v,n]=a
s.n=v;
endfor
endfor
endfunction
but I get an error in the first 'for'---you can't iterate like
that. OK. so I thought this might work:
function s=combine(varargin)
for i=1:nargin
a=varargin{i}
for [v,n]=a
s.n=v
endfor
endfor
endfunction
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?