function srd = srdRepack(srd,data)
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% We want to reconstitute srd from a vector of structs into one struct.
%
% If srd is a struct array, then do the following:
% a)  For a given field in srd(i), e.g., srd(i).field, concatenate 
%     the data across i=1,2,...,n into a matrix, i.e., 
%     Mi=[srd(1).field; srd(2).field; ...; srd(n).field]. 
% b)  Repeat for each field in srd.
% 
%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

%--------------------------------------------------------------------------
% Collate or Repack the array of structs into one struct
%
srdcopy = srd;
srd=[]; 
n = length(srdcopy);
if n > 1
    F = fieldnames(srdcopy);
    % Cycle through each field name in srd
    for i = 1:length(F)
        % Cycle through each component of srd associated with field F{i}
        % and extract the data into cell array components
        for j = 1:n
            C = arrayfun(@(x)(x.(F{i})), srdcopy, 'Un', false);
            try 
                srd.(F{i}) = cell2mat(C(:));
            catch
                srd.(F{i}) = nanstack(C(:), true);  
            end    
        end
    end    
else
    srd = srdcopy; 
end    

%--------------------------------------------------------------------------
% Apppend data to srd struct for posterity 
%
srd.data = data;
%==========================================================================
end
