0001 function data = dsMdd2ds(obj,varargin)
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 if isempty(obj.findaxis('variables'))
0032 Na = length(obj.axis);
0033 obj.axis(Na+1).name = 'variables';
0034 obj.axis(Na+1).values = {guess_variable_name(obj)};
0035 end
0036
0037
0038 if isempty(obj.findaxis('populations'))
0039 Na = length(obj.axis);
0040 obj.axis(Na+1).name = 'populations';
0041 obj.axis(Na+1).values = {guess_population_name(obj)};
0042 end
0043
0044
0045 obj = obj.squeezeRegexp('Dim');
0046
0047
0048 pop_axis = obj.findaxis('populations');
0049 var_axis = obj.findaxis('variables');
0050
0051
0052 varied_inds = true(1,ndims(obj)); varied_inds(pop_axis) = false; varied_inds(var_axis) = false;
0053 varied_axis = find(varied_inds);
0054 has_varied = ~isempty(varied_axis);
0055
0056
0057 obj = obj.permute([pop_axis,var_axis, varied_axis(:)']);
0058
0059
0060 num_pops = size(obj,1);
0061 pop_names = obj.exportAxisVals; pop_names = pop_names{1};
0062
0063
0064
0065
0066 obj = obj.mergeDims(1:2);
0067
0068
0069 if has_varied
0070 obj = obj.mergeDims(3:ndims(obj));
0071 end
0072
0073
0074
0075
0076 obj = obj.squeezeRegexp('Dim');
0077
0078
0079
0080
0081 data = struct;
0082 ax_vals = obj.exportAxisVals;
0083 ax_names = obj.exportAxisNames;
0084 if has_varied
0085 varied = obj.axis(2).axismeta.premerged_names;
0086 varied_vals = obj.axis(2).axismeta.premerged_values;
0087 end
0088
0089 for j = 1:size(obj,2)
0090
0091
0092 for i = 1:size(obj,1)
0093 data(j).(ax_vals{1}{i}) = obj.data{i,j};
0094 end
0095
0096
0097 if has_varied
0098
0099 data(j).varied = varied;
0100
0101
0102 for i = 1:length(varied)
0103 if isnumeric(varied_vals{i}(j))
0104 data(j).(varied{i}) = varied_vals{i}(j);
0105 else
0106 data(j).(varied{i}) = varied_vals{i}{j};
0107 end
0108 end
0109 end
0110
0111
0112 obj_curr = obj.meta.dynasim;
0113
0114 fc = 'model'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0115 fc = 'simulator_options'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0116 fc = 'time'; if isfield(obj_curr,fc); data(j).(fc) = obj_curr.(fc); end
0117 end
0118
0119
0120 labels = get_axis_labels(obj,ax_vals);
0121 for j = 1:length(data)
0122 data(j).labels = labels;
0123 end
0124
0125
0126 data = add_pop_sizes(data,obj,num_pops,pop_names);
0127
0128 data = dsCheckData(data, varargin{:});
0129
0130
0131 end
0132
0133
0134 function varied_names = only_varieds(all_names)
0135 inds = true(1,length(all_names));
0136 inds(strcmp(all_names,'populations')) = false;
0137 inds(strcmp(all_names,'variables')) = false;
0138 varied_names = all_names(inds);
0139 end
0140
0141 function data = add_pop_sizes(data,obj,num_pops,pop_names)
0142
0143 for i = 1:length(data)
0144 for j = 1:num_pops
0145 obj_temp = obj(['/^' pop_names{j} '/'],i);
0146
0147
0148 pop_sz = cellfun(@(x) size(x,2),obj_temp.data);
0149
0150
0151
0152 var_names = obj_temp.axis('populations_variables').values;
0153 num_vars = length(var_names);
0154 ind = regexp(lower(var_names),'_v$||_vm$||_x$||_xm$||_y$||_ym$');
0155 ind = ~cellfun(@isempty,ind);
0156 if all(ind == 0)
0157
0158
0159
0160
0161
0162 ind = true(1,num_vars);
0163 for k = 1:num_pops
0164 if k == j; continue; end
0165 searchstr = ['^' pop_names{j} '_' pop_names{k} '_'];
0166 ind3 = regexp(var_names,searchstr);
0167 ind3 = ~cellfun(@isempty,ind3);
0168 ind(ind3) = false;
0169 end
0170 ind = ind & pop_sz(:)' ~= 0;
0171
0172 end
0173
0174
0175 pop_sz2 = pop_sz(ind);
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 data(i).model.specification.populations(j).size = mode(pop_sz2);
0186 end
0187 end
0188 end
0189
0190
0191 function labels = get_axis_labels(obj,ax_vals)
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 labels_orig = obj.meta.dynasim.labels;
0202 labels_curr = {ax_vals{1}{:}, 'time'};
0203 ind_keep = false(1,length(labels_orig));
0204 ind_remove = true(1,length(labels_curr));
0205 for i = 1:length(labels_curr)
0206
0207 ind_temp = strcmp(labels_orig,labels_curr{i});
0208 ind_keep = ind_keep | ind_temp;
0209
0210
0211 if any(ind_temp)
0212 ind_temp = strcmp(labels_curr,labels_curr{i});
0213 ind_remove(ind_temp) = 0;
0214 end
0215 end
0216
0217 labels_orig = labels_orig(ind_keep);
0218 labels_curr = labels_curr(ind_remove);
0219 labels = {labels_orig{:} labels_curr{:}};
0220
0221
0222
0223
0224
0225
0226 if strcmp(labels{1},'time')
0227 labels = circshift(labels,-1);
0228 end
0229
0230 labels = labels(:)';
0231
0232
0233
0234 end
0235
0236
0237 function out = guess_variable_name(obj)
0238
0239
0240
0241 out = dsGet_variables_from_meta(obj);
0242 out = out{1};
0243 if isempty(out)
0244 out = 'v';
0245 end
0246
0247 end
0248
0249
0250
0251 function out = guess_population_name(obj)
0252
0253
0254
0255 out = dsGet_populations_from_meta(obj);
0256 out = out{1};
0257
0258 if isempty(out)
0259 out = 'E';
0260 end
0261
0262 end