function [nstruct] = structsadd(ostruct, addstruct)
% add the fields in otruct and addstruct
fno=fieldnames(ostruct);
for i=1:size(fno,1)
    field_name=fno{i};
    %     cmd=sprintf('tmp=ostruct.%s', field_name);
    %     eval(cmd);
    %     [r,c]=size(tmp);

    eval( [' [ro,co]=size(ostruct.' field_name ');' ]);
    if isfield(addstruct, field_name),
        eval( ['[ rn,cn]=size(addstruct.' field_name ');']);
        if ro > 1 && co == 1
            if rn > 1 && cn == 1,
                % case 1: old 100,1  new 100,1
                cmd=sprintf('nstruct.%s = [ostruct.%s'' addstruct.%s'' ];',...
                    field_name,field_name,field_name);
            else
                % case 2: old 100,1   new 1,100
                cmd=sprintf('nstruct.%s = [ostruct.%s'' addstruct.%s ];',...
                    field_name,field_name,field_name);
            end
        else
            if rn > 1 && cn == 1,
                % case 3: old 1,100  new 100,1
                cmd=sprintf('nstruct.%s = [ostruct.%s addstruct.%s'' ];',...
                    field_name,field_name,field_name);
            else
                % case 4: old 1,100  new 1,100
                cmd=sprintf('nstruct.%s = [ostruct.%s addstruct.%s ];',...
                    field_name,field_name,field_name);
            end
        end
    else
        % case 5: old only, not a field in new
        cmd=sprintf('nstruct.%s = [ostruct.%s;',field_name,field_name);
        %cmd=['nstruct.' field_name ' =  [ ostruct.' field_name ' addstruct.' field_name '];'];
    end
    eval(cmd);
end
%cmd=['nstruct.' field_name ...
%      ' = [ ostruct.' field_name ' addstruct.' field_name '];'];

