% function [nstruct] = struct_merge(instruct1, instruct2)
% combine 2 structs with identical fileds; stuct 1 before struct 2
% fields in struct2 that are not ins truct 1 are lost!
% 20150227 deal with diffeerently oriented arrays
function [nstruct] = struct_merge(instruct1, instruct2)
debug=0;
nstruct=instruct1;
% loop through all the fields in the structure
fno=fieldnames(instruct1);
for i=1:length(fno)
    fn=fno{i};
    if debug ==1,
        fprintf('%s ',fn); end
    
    % check size so you can concatenate
    eval(['jjjj = nstruct.' fn ';']);
    [d1 d2]=size(jjjj);
    if d1 == 1
        cmd=['nstruct.' fn ' = [ nstruct.' fn ' instruct2.' fn '];' ];
    elseif d2 == 1
        cmd=['nstruct.' fn ' = [ nstruct.' fn '; instruct2.' fn '];' ];
    else
        fprintf('%s is a 2d array, not merging\n',fn)
    end
    eval (cmd);
end
