Home > database > ensemble_merge_data.m

ensemble_merge_data

PURPOSE ^

Merges data from multiple structs into one struct.

SYNOPSIS ^

function outData = ensemble_merge_data(inData,params)

DESCRIPTION ^

 Merges data from multiple structs into one struct.

 outData = ensemble_merge_data(inData,params)


 accepts a cell array of multiple ensemble data structs, where the
 data structs are all of the same type, with the same number of
 vars and matching var names.

 The data from each struct is merged into a single struct and
 returned

 Parameters supported:
    uniqueKeyField: (string) specifies a variable name which
                             identifies unique row
                             information. This is useful for merging
                             multiple data structs with
                             overlapping information when there
                             is a key field that describes
                             repeated row information (e.g. stimulus_id)

 5/24/07 - First Version, S.T.
 8/13/07 - S.T. added support for uniqueKeyField

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function outData = ensemble_merge_data(inData,params)
0002 % Merges data from multiple structs into one struct.
0003 %
0004 % outData = ensemble_merge_data(inData,params)
0005 %
0006 %
0007 % accepts a cell array of multiple ensemble data structs, where the
0008 % data structs are all of the same type, with the same number of
0009 % vars and matching var names.
0010 %
0011 % The data from each struct is merged into a single struct and
0012 % returned
0013 %
0014 % Parameters supported:
0015 %    uniqueKeyField: (string) specifies a variable name which
0016 %                             identifies unique row
0017 %                             information. This is useful for merging
0018 %                             multiple data structs with
0019 %                             overlapping information when there
0020 %                             is a key field that describes
0021 %                             repeated row information (e.g. stimulus_id)
0022 %
0023 % 5/24/07 - First Version, S.T.
0024 % 8/13/07 - S.T. added support for uniqueKeyField
0025 
0026 % if only a single data struct was passed in, just return the data
0027 % struct with no changes.
0028 if(length(inData) == 1)
0029   outData = inData;
0030   return
0031 end
0032 
0033 
0034 
0035 outData = ensemble_init_data_struct;
0036 outData.vars = inData{1}.vars;
0037 outData.data = cell(size(outData.vars));
0038 outVarNames = outData.vars;
0039 outDataCols = set_var_col_const(outData.vars);
0040 outData.type = inData{1}.type;
0041 
0042 for iStruct = 1:length(inData)
0043   
0044   if(~isempty(setxor(inData{iStruct}.vars,outData.vars)))
0045     error('All data structs must have matching vars.');
0046   end
0047   
0048   inDataCols = set_var_col_const(inData{iStruct}.vars);
0049   
0050   if(isfield(params,'uniqueKeyField'))
0051     uniqueField = params.uniqueKeyField;
0052     [keyVals,keyIdxs] = setdiff(inData{iStruct}.data{inDataCols.(uniqueField)},...
0053                 outData.data{outDataCols.(uniqueField)});
0054   end  
0055   
0056   for iData = 1:length(outVarNames)
0057     
0058     if(~isfield(params,'uniqueKeyField'))
0059       nRows = size(inData{iStruct}.data{inDataCols.(outVarNames{iData})},1);
0060       rowIdxs = 1:nRows;
0061     else
0062       nRows = length(keyIdxs);
0063       rowIdxs = keyIdxs;
0064     end
0065     
0066     %if the data is a row vector, then it will be appended a row at
0067     %a time, so that we end up with a matrix. If it is a column
0068     %vector, we'll just append the columns together so they become
0069     %a long column matrix. This is sort of a cheap and easy way of dealing
0070     %with whether you want to end up with a matrix or a long
0071     %vector.
0072     nCols = size(inData{iStruct}.data{inDataCols.(outVarNames{iData})},2);
0073     colIdxs = 1:nCols;
0074     
0075     if(nRows > 0)
0076       %vars might not be in the same order in the struct,
0077       %so deal with that here
0078       outData.data{outDataCols.(outVarNames{iData})}(end+1:end+nRows,1:nCols) = ...
0079       inData{iStruct}.data{inDataCols.(outVarNames{iData})}(rowIdxs,colIdxs);
0080     end
0081     
0082   end
0083   
0084 end
0085 
0086 
0087 
0088 
0089 
0090 
0091

Generated on Wed 20-Mar-2019 04:00:51 by m2html © 2003