adds a given row of data to a given ensemble data struct REQUIRES indata - ensemble data struct varargin - key/value pairs representing a row of data 'key' - always a string matching a column name in indata.vars 'value' - could be any type, is added as the value of 'key' NOTE: all values are appended to the end of each existing row NOTE: fills out short columns with empty values of the appropriate type FIXME: allow addition of multiple rows? pass in vectors instead of scalar or single-item types? how would we differentiate vectors for rows and vectors that should be an individual indexed item in indata? any varargin switch or keyword would have to be something that would never ever ever be a variable name in an ensemble data struct... FB 2009.03.09 PJ 2010.11.08 - fixed handling of single items in that were not cell arrays and which generated an excess number of rows by virtue of their length.
0001 function indata = ensemble_add_data_struct_row(indata,varargin) 0002 0003 % adds a given row of data to a given ensemble data struct 0004 % 0005 % REQUIRES 0006 % indata - ensemble data struct 0007 % varargin - key/value pairs representing a row of data 0008 % 'key' - always a string matching a column name in indata.vars 0009 % 'value' - could be any type, is added as the value of 'key' 0010 % 0011 % NOTE: all values are appended to the end of each existing row 0012 % NOTE: fills out short columns with empty values of the appropriate type 0013 % 0014 % FIXME: allow addition of multiple rows? pass in vectors instead of scalar 0015 % or single-item types? how would we differentiate vectors for rows and 0016 % vectors that should be an individual indexed item in indata? any varargin 0017 % switch or keyword would have to be something that would never ever ever 0018 % be a variable name in an ensemble data struct... 0019 % 0020 % FB 2009.03.09 0021 % PJ 2010.11.08 - fixed handling of single items in that were not cell arrays and 0022 % which generated an excess number of rows by virtue of 0023 % their length. 0024 0025 if ~is_ensemble_datastruct(indata) 0026 error('indata is not a valid ensemble data structure\n'); 0027 end 0028 0029 % get var names 0030 cols = set_var_col_const(indata.vars); 0031 0032 % add row 0033 for iv = 1:2:nargin-1 0034 fld = varargin{iv}; 0035 if isfield(cols,fld) 0036 % if iscell(indata.data{cols.(fld)}) 0037 % indata.data{cols.(fld)}{end+1} = varargin{iv+1}; 0038 % else 0039 % inclass = class(indata.data{cols.(fld)}); 0040 % vclass = class(varargin{iv+1}); 0041 % if isempty(strmatch(inclass,vclass)) 0042 % error(['data struct column type (%s) and value type (%s) for '... 0043 % '%s key do not match'],inclass,vclass,fld); 0044 % else 0045 indata.data{cols.(fld)} = [indata.data{cols.(fld)}; varargin{iv+1}]; 0046 % indata.data{cols.(fld)}(end+1) = varargin{iv+1}; 0047 % indata.data{cols.(fld)} = [ 0048 % end 0049 % end 0050 end % if isfield(cols, 0051 end % for ivar = 0052 0053 % find maximum column length 0054 %maxn = max(cellfun(@length,indata.data)); 0055 dims = cellfun(@size,indata.data,'UniformOutput',false); 0056 dims = cat(1,dims{:}); 0057 maxn = max(dims(:,1)); 0058 0059 % fill out short columns 0060 for id = 1:length(indata.data) 0061 while length(indata.data{id}) < maxn 0062 if iscell(indata.data{id}) 0063 if isempty(indata.data{id}) 0064 indata.data{id} = cell(maxn,1); 0065 else 0066 indata.data{id} = [indata.data{id}; ... 0067 cell(maxn-length(indata.data{id}),1)]; 0068 end 0069 else 0070 if isempty(indata.data{id}) 0071 indata.data{id} = nan(maxn,1); 0072 else 0073 indata.data{id} = [indata.data{id}; ... 0074 nan(maxn-length(indata.data{id}),1)]; 0075 end 0076 end 0077 end 0078 end