s=mergestruct(s1,s2) merge two struct objects into one authors:Qianqian Fang (q.fang <at> neu.edu) date: 2012/12/22 input: s1,s2: a struct object, s1 and s2 can not be arrays output: s: the merged struct object. fields in s1 and s2 will be combined in s. license: BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
0001 function s=mergestruct(s1,s2) 0002 % 0003 % s=mergestruct(s1,s2) 0004 % 0005 % merge two struct objects into one 0006 % 0007 % authors:Qianqian Fang (q.fang <at> neu.edu) 0008 % date: 2012/12/22 0009 % 0010 % input: 0011 % s1,s2: a struct object, s1 and s2 can not be arrays 0012 % 0013 % output: 0014 % s: the merged struct object. fields in s1 and s2 will be combined in s. 0015 % 0016 % license: 0017 % BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 0018 % 0019 % -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) 0020 % 0021 0022 if(~isstruct(s1) || ~isstruct(s2)) 0023 error('input parameters contain non-struct'); 0024 end 0025 if(length(s1)>1 || length(s2)>1) 0026 error('can not merge struct arrays'); 0027 end 0028 fn=fieldnames(s2); 0029 s=s1; 0030 for i=1:length(fn) 0031 s=setfield(s,fn{i},getfield(s2,fn{i})); 0032 end 0033