function x = catstruct(a,b) % function x = catstruct(a,b) % % T.Hilmer, UH % concatenates two structures, a & b % useful for loading data from (dis)similar files % % if fields don't match, will simply copy fields % x = load('fileA'); x = catstruct(x,load('fileB')); % % if fields match, will try to concatenate along only mismatched dimension, % and if all dimensions match, will concatenate along first dimension % ~ 2010.01.01 version 1 if any(size(a) ~= size(b)), error('this fcn only written for scalar structures'), end fa = fieldnames(a); fb = fieldnames(b); x = a;% could replace 'x' with 'a' for memory..... N = length(fb); for n = 1:N f = fb{n}; if any(strcmp(fa,f)) dim = find(size(x.(f))~=size(b.(f))); if isempty(dim) x.(f) = cat(1,x.(f),b.(f)); elseif ~isscalar(dim), error(['impossible to concatenate field ''' f ''' because more than one dimension is mismatched']) else x.(f) = cat(dim,x.(f),b.(f)); end else x.(f) = b.(f); end end