Home > matutils > mergestruct.m

mergestruct

PURPOSE ^

function s=mergestruct(s,b)

SYNOPSIS ^

function s=mergestruct(s,b,field)

DESCRIPTION ^

 function s=mergestruct(s,b)

 merges the fields of structure b into struct s.  it will only do it
 for fields of b that is not present in s.

 For example:
  s(i).a.b.c to s.a.b.c(i)

Author: Michael Loh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function s=mergestruct(s,b,field)
0002 
0003 % function s=mergestruct(s,b)
0004 %
0005 % merges the fields of structure b into struct s.  it will only do it
0006 % for fields of b that is not present in s.
0007 %
0008 % For example:
0009 %  s(i).a.b.c to s.a.b.c(i)
0010 %
0011 %Author: Michael Loh
0012 
0013 
0014 if (~isstruct(b))
0015   return
0016 end
0017 
0018 if (nargin==2)
0019   field=[];
0020 end
0021 
0022 if (~isempty(field))
0023   eval(sprintf('n=fieldnames(b.%s);',field));
0024 else
0025   n=fieldnames(b);
0026 end
0027 
0028 for i=1:length(n)
0029   if (isempty(field))
0030     newfield=n{i};
0031   else
0032     newfield=strcat(field,'.',n{i});
0033   end
0034   eval(sprintf('arg=isstruct(b.%s);',newfield));
0035   if (arg)
0036     s=mergestruct(s,b,newfield);
0037   else
0038     eval(sprintf('s.%s=b.%s;',newfield,newfield));
0039   end
0040 end

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005