0001 function data = dsCalcSumOverFields(data, fields, output_field_name, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
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];
0023 end
0024
0025 if nargin < 3
0026
0027 output_field_name = 'summed';
0028
0029
0030 ind = strfind(fields{1},'_');
0031 prefix = fields{1}(1:ind);
0032 output_field_name = strcat(prefix,output_field_name);
0033 end
0034
0035
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
0046
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}};
0056 end
0057
0058
0059 if options.auto_gen_test_data_flag
0060 argout = {data};
0061
0062 dsUnitSaveAutoGenTestData(argin, argout);
0063 end
0064
0065 end