mergestruct.m 771 B

123456789101112131415161718192021222324252627282930313233
  1. function s=mergestruct(s1,s2)
  2. %
  3. % s=mergestruct(s1,s2)
  4. %
  5. % merge two struct objects into one
  6. %
  7. % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
  8. % date: 2012/12/22
  9. %
  10. % input:
  11. % s1,s2: a struct object, s1 and s2 can not be arrays
  12. %
  13. % output:
  14. % s: the merged struct object. fields in s1 and s2 will be combined in s.
  15. %
  16. % license:
  17. % BSD, see LICENSE_BSD.txt files for details
  18. %
  19. % -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
  20. %
  21. if(~isstruct(s1) || ~isstruct(s2))
  22. error('input parameters contain non-struct');
  23. end
  24. if(length(s1)>1 || length(s2)>1)
  25. error('can not merge struct arrays');
  26. end
  27. fn=fieldnames(s2);
  28. s=s1;
  29. for i=1:length(fn)
  30. s=setfield(s,fn{i},getfield(s2,fn{i}));
  31. end