Home > functions > internal > dsCalcSumOverFields.m

dsCalcSumOverFields

PURPOSE ^

CALCSUMOVERFIELDS - Creates a new field in data that is the sum of a bunch of other fields

SYNOPSIS ^

function data = dsCalcSumOverFields(data, fields, output_field_name, varargin)

DESCRIPTION ^

CALCSUMOVERFIELDS - Creates a new field in data that is the sum of a bunch of other fields

 These fields are specified by the "fields" cell array. Stores the output in
 "output_field_name." Useful for adding multiple ionic currents together.

 Inputs:
   - data: DynaSim data structure (see dsCheckData)
   - fields: cell array of field names. These will be summed and stored in the
       field called output_field_name.
   - output_field_name: string containing the desired output field name.

 Outputs:
   - data: data structure containing summed data

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function data = dsCalcSumOverFields(data, fields, output_field_name, varargin)
0002 %CALCSUMOVERFIELDS - Creates a new field in data that is the sum of a bunch of other fields
0003 %
0004 % These fields are specified by the "fields" cell array. Stores the output in
0005 % "output_field_name." Useful for adding multiple ionic currents together.
0006 %
0007 % Inputs:
0008 %   - data: DynaSim data structure (see dsCheckData)
0009 %   - fields: cell array of field names. These will be summed and stored in the
0010 %       field called output_field_name.
0011 %   - output_field_name: string containing the desired output field name.
0012 %
0013 % Outputs:
0014 %   - data: data structure containing summed data
0015 
0016 %% auto_gen_test_data_flag argin
0017 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0018 if options.auto_gen_test_data_flag
0019   varargs = varargin;
0020   varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0021   varargs(end+1:end+2) = {'unit_test_flag',1};
0022   argin = [{data}, {fields}, {output_field_name}, varargs]; % specific to this function
0023 end
0024 
0025 if nargin < 3
0026     % Default field name
0027     output_field_name = 'summed';
0028 
0029     % Add default prefix
0030     ind = strfind(fields{1},'_');
0031     prefix = fields{1}(1:ind);
0032     output_field_name = strcat(prefix,output_field_name);
0033 end
0034 
0035 % Add prefix if necessary
0036 if isempty(strfind(output_field_name,'_'))
0037     warning('No population prefix specified (see documentation). Adding a default prefix.');
0038     ind = strfind(fields{1},'_');
0039     prefix = fields{1}(1:ind);
0040     output_field_name = strcat(prefix,output_field_name);
0041 
0042 end
0043 
0044 data = dsCheckData(data, varargin{:});
0045 % note: calling dsCheckData() at beginning enables analysis function to
0046 % accept data matrix [time x cells] in addition to DynaSim data structure.
0047 
0048 for i = 1:length(data)
0049     dat = data(i);
0050     sumfields = zeros(size(dat.(fields{1})));
0051     for j = 1:length(fields)
0052         sumfields = sumfields + dat.(fields{j});
0053     end
0054     data(i).(output_field_name) = sumfields;
0055     data(i).labels(end:end+1) = {output_field_name, data(i).labels{end}};   % Store new label as 2nd last entry (so time is always last; not sure if this is important...
0056 end
0057 
0058 %% auto_gen_test_data_flag argout
0059 if options.auto_gen_test_data_flag
0060   argout = {data}; % specific to this function
0061   
0062   dsUnitSaveAutoGenTestData(argin, argout);
0063 end
0064 
0065 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005