IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
data_merge_rows.m
Go to the documentation of this file.
1 %>@ingroup datasettools
2 %> @file
3 %> @brief Merges several datasets into one (row-wise)
4 %>
5 %> The new dataset ('out') will be first created as a clone of datasets(1) however with the fields listed in datasets(1).rowfieldnames empty
6 %>
7 %> Needless say the datasets need to be column-compatible.
8 
9 function out = data_merge_rows(datasets)
10 
11 newlabels = unique_appear([datasets.classlabels]);
12 
13 % prepares output based on first dataset
14 out = datasets(1).copy_emptyrows();
15 out.classlabels = newlabels;
16 % Prepares list of fields to merge, except for classes, which is a more complicated case
17 rr = datasets(1).rowfieldnames;
18 nr = numel(rr);
19 iclasses = find(strcmp('classes', rr));
20 nd = numel(datasets);
21 
22 flags = ones(1, nr);
23 for i = 1:nr % Determines which fields to merge and which to ignore.
24  for j = 1:nd
25  if size(datasets(j).(rr{i}), 1) < datasets(j).no
26  % If a row field has less elements than the dataset's no property, the field is excluded from the merge.
27  flags(i) = 0;
28  break;
29  end;
30  end;
31 end;
32 
33 % Pre-allocation
34 no_all = sum([datasets.no]);
35 for j = 1:nr
36  if flags(j)
37  if out.flags_cell(j)
38  out.(rr{j}) = cell(no_all, size(datasets(1).(rr{j}), 2));
39  else
40  out.(rr{j}) = zeros(no_all, size(datasets(1).(rr{j}), 2));
41  end;
42  end;
43 end;
44 % out.classes = zeros(no_all, 1);
45 
46 
47 ptr = 1;
48 for i = 1:nd
49  data = datasets(i);
50 
51  % Finds new class indexes
52 % newidxs = zeros(1, data.nc);
53  newclasses = data.classes;
54  for j = 1:data.nc
55  newclass = find(strcmp(data.classlabels{j}, newlabels))-1;
56  newclasses(data.classes == j-1) = newclass;
57  end;
58 
59  % merges the rowfieldnames fields (except 'classes')
60  for j = 1:nr
61  if flags(j)
62  if j == iclasses
63  out.classes(ptr:ptr+data.no-1, :) = newclasses;
64  else
65 % out.(rr{j}) = [out.(rr{j}); data.(rr{j})];
66  out.(rr{j})(ptr:ptr+data.no-1, :) = data.(rr{j});
67  end;
68  end;
69  end;
70  ptr = ptr+data.no;
71 end;
Analysis Session (AS) base class.
Definition: as.m:6
function unique_appear(in classlabels)
function data_merge_rows(in datasets)