IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
wire2data.m
Go to the documentation of this file.
1 %>@ingroup ioio
2 %>@file
3 %> @brief Merges several "Wire txt" files into a dataset
4 %>
5 %> @c data.groupcodes is made from file name. File name is trimmed at the trimdot-th dot counted from right to left. E.g.,
6 %> This allows one to trim "sample.0.dat" at the penultimate dot (ignodedoctount=2) or "sample.dat" at the last dot
7 %> (trimdot=1)
8 %>
9 %> data.obsnames will contain the file names
10 %>
11 %> For reference on parameters, please check @ref mergetool.m
12 %>
13 %> @sa mergetool.m
14 
15 %> @param wild
16 %> @param trimdot See pirtool
17 %> @param flag_image
18 %> @param height
19 %> @return A dataset
20 function [data, flag_error] = wire2data(wild, trimdot, flag_image, height)
21 
22 if ~exist('trimdot', 'var')
23  trimdot = 2;
24 end;
25 
26 [filenames, groupcodes] = resolve_dir(wild, trimdot, flag_image);
27 
28 no_files = length(filenames);
29 
30 if flag_image
31  if no_files/height ~= floor(no_files/height)
32  irerror('Wire2Data: Invalid image height!');
33  end;
34 end;
35 
36 
37 idxs = find(wild == '/');
38 if isempty(idxs)
39  path_ = '';
40 else
41  path_ = wild(1:idxs(end));
42 end;
43 data = irdata();
44 
45 flag_first = 1;
46 ipro = progress2_open('WIRE2DATA', [], 0, no_files);
47 cnt_error = 0;
48 errors = {};
49 ii = 0;
50 for i = 1:no_files
51  filename = fullfile(path_, filenames{i});
52  try
53  M = dlmread(filename);
54 
55  if flag_first
56  wns = M(:, 1)';
57  nf = numel(wns);
58  else
59  % Checks against previous size
60  n1 = size(M, 1);
61  if ~(n1 == nf)
62  irerror(sprrintf('Wire2Data: Wrong number of data points: expected=%d; found=%d', nf, n1));
63  end;
64  end;
65 
66  x = M(:, 2)';
67 
68  if flag_first
69  % Initializes dataset if first row
70  data.X = zeros(no_files, nf);
71  data.classes = zeros(no_files, 1);
72  data.obsnames = cell(no_files, 1);
73  data.groupcodes = cell(no_files, 1);
74  data.fea_x = wns;
75  data.classlabels = {'Class 0'};
76  flag_first = 0;
77  end;
78 
79  ii = ii+1;
80  data.X(ii, :) = x;
81  data.classes(ii) = 0;
82  data.obsnames{ii} = filenames{i};
83  data.groupcodes{ii} = groupcodes{i};
84  catch ME
85  irverbose(['ERROR: ', ME.message]);
86  irverbose(ME.getReport());
87  cnt_error = cnt_error+1;
88  errors{end+1} = filename;
89  end;
90  ipro = progress2_change(ipro, [], [], i);
91 end;
92 progress2_close(ipro);
93 
94 flag_error = 0;
95 if cnt_error > 0
96  irverbose(sprintf('NOTICE: only %d/%d files were successfully read. Import failed on following files:', no_files-cnt_error, no_files));
97  for i = 1:cnt_error
98  irverbose(errors{i});
99  end;
100  flag_error = cnt_error;
101 
102  % Trims matrices rows because were overdimensioned
103  data.X = data.X(1:ii, :);
104  data.classes = data.classes(1:ii, :);
105  data.obsnames = data.obsnames(1:ii, :);
106  data.groupcodes = data.groupcodes(1:ii, :);
107 end;
108 
109 
110 if flag_image
111  data.height = height;
112 end;
113 
function irverbose(in s, in level)
function progress2_change(in prgrss, in title, in perc, in i, in n)
function progress2_open(in title, in perc, in i, in n)
function irerror(in s)
Dataset class.
Definition: irdata.m:30
function resolve_dir(in wild, in trimdot, in flag_image)
function wire2data(in wild, in trimdot, in flag_image, in height)
function progress2_close(in prgrss)