0001 function result_st = ensemble_concat_datastruct(data_st,params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 if (iscell(data_st) && ~isempty(data_st) && isfield(data_st{1},'task') && ...
0037 ~isempty(strmatch('return_outdir',data_st{1}.task))) || ...
0038 (isstruct(data_st) && isfield(data_st,'task') && ...
0039 ~isempty(strmatch('return_outdir',data_st.task)))
0040 result_st = '';
0041 return
0042 end
0043
0044 if nargin < 2
0045 params = struct;
0046 end
0047
0048 result_st = ensemble_init_data_struct;
0049 if isfield(params,'outDataName')
0050 result_st.name=params.outDataName;
0051 else
0052 result_st.name='concat_datastruct';
0053 end
0054
0055 if isfield(params,'outDataType')
0056 result_st.type=params.outDataType;
0057 else
0058 result_st.type='concat_datastruct';
0059 end
0060
0061 nstruct = length(data_st);
0062
0063
0064
0065
0066 if isstruct(data_st)
0067 old_data = data_st;
0068 data_st = {};
0069 for id=1:nstruct
0070 data_st{id} = old_data(id);
0071 end
0072 end
0073
0074
0075
0076
0077 if isfield(params,'type_as_var')
0078 type_as_var = params.type_as_var;
0079 else
0080 type_as_var = false;
0081 end
0082
0083 if isfield(params,'append_concat_to_name')
0084 append_concat_to_name = params.append_concat_to_name;
0085 else
0086 append_concat_to_name = true;
0087 end
0088
0089
0090 if ~type_as_var
0091
0092
0093 vars = data_st{1}.vars;
0094 nvars = length(vars);
0095
0096 result_st.vars = vars;
0097 result_st.data = data_st{1}.data;
0098
0099 if nstruct < 2
0100 return
0101 end
0102
0103 for istruct = 2:nstruct
0104 for ivar = 1:nvars
0105 if ~strcmp(data_st{istruct}.vars{ivar},vars{ivar})
0106 fprintf('%s: variable mismatch: Found %s, expected %s\n', mfilename, ...
0107 data_st{istruct}.vars{ivar}, vars{ivar});
0108 result_st = ensemble_init_data_struct;
0109 return
0110 else
0111 if isempty(data_st{istruct}.data{ivar})
0112 error('Trying to concatenate empty data')
0113 end
0114 result_st.data{ivar} = [result_st.data{ivar}; data_st{istruct}.data{ivar}];
0115 end
0116 end
0117 end
0118
0119 else
0120
0121 struct_types = cellfun(@getfield,data_st, ...
0122 ...
0123 repmat({'type'},size(data_st)),'UniformOutput',false);
0124
0125 unique_types = unique(struct_types);
0126 num_types = length(unique_types);
0127
0128 result_st.vars = unique_types;
0129 for itype = 1:num_types
0130 curr_type = unique_types{itype};
0131
0132
0133 curr_st = ensemble_init_data_struct;
0134 if append_concat_to_name
0135 curr_st.name = sprintf('%s_concat', curr_type);
0136 else
0137 curr_st.name = sprintf('%s', curr_type);
0138 end
0139 curr_st.type = curr_type;
0140
0141
0142 st_idxs = strmatch(curr_type, struct_types);
0143
0144
0145 vars = data_st{st_idxs(1)}.vars;
0146 nvars = length(vars);
0147 curr_st.vars = vars;
0148
0149 curr_st.data = sanitize_char_data(data_st{st_idxs(1)}.data);
0150
0151
0152 for istruct = 2:length(st_idxs)
0153 for ivar = 1:nvars
0154 if ~strcmp(data_st{st_idxs(istruct)}.vars{ivar}, vars{ivar})
0155 fprintf('%s: variable mismatch: Found %s, expected %s\n', mfilename, ...
0156 data_st{st_idxs(istruct)}.vars{ivar}, vars{ivar});
0157 result_st = ensemble_init_data_struct;
0158 return
0159 else
0160 curr_st.data{ivar} = [curr_st.data{ivar}; ...
0161 sanitize_char_data(data_st{st_idxs(istruct)}.data{ivar})];
0162 end
0163 end
0164 end
0165
0166 result_st.data{itype} = curr_st;
0167 end
0168 end
0169 end
0170
0171 function data = sanitize_char_data(data)
0172
0173 if iscell(data)
0174 nvars = length(data);
0175 else
0176 nvars = 1;
0177 end
0178 for ivar = 1:nvars
0179 if iscell(data) && ischar(data(ivar))
0180 data{ivar} = {data(ivar)};
0181 elseif ischar(data)
0182 data = {data};
0183 end
0184 end
0185 end