IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
detect_file_type.m
Go to the documentation of this file.
1 %>@ingroup datasettools
2 %> @file
3 %> @brief does a series of tests on the dataset file to try to guess its format
4 function classname = detect_file_type(filename)
5 
6 if ~exist(filename, 'file')
7  irerror(sprintf('File "%s" does not exist!', filename));
8 end;
9 
10 classname = '';
11 id = fopen(filename, 'r');
12 
13 if id <= 0
14  irerror(sprintf('Could not open file "%s"', filename));
15 end;
16 
17 try
18 
19  while 1
20  % checks MAT
21  s = fread(id, 6);
22  if strcmp(char(s)', 'MATLAB')
23  classname = 'dataio_mat';
24  break;
25  end;
26 
27  fseek(id, 0, 'bof');
28  s = fread(id, 8);
29  s = strip_quotes(char(s)');
30  if length(s) >= 7
31  if strcmp(lower(s(1:7)), 'irtools') || strcmp(lower(s(1:5)), 'iroot')
32  classname = 'dataio_txt_irootlab';
33  break;
34  end;
35  end;
36 
37  fseek(id, 0, 'bof');
38  s = fread(id, 1);
39  if s >= 48 && s <= 57 || s == '-'
40  % If contents starts with a number, the file type is likely "basic"
41  classname = 'dataio_txt_basic';
42  break;
43  end;
44 
45  if sum(s == sprintf('\t,; ')) > 0 % Tests CSV common separators
46  % If file starts with a separator, file type is likely to be the Pirouette table
47  classname = 'dataio_txt_pir';
48  break;
49  end;
50 
51  fseek(id, 0, 'bof');
52  s = fread(id, 4);
53  if sum(s == [10, 10, 254, 254]') == 4
54  classname = 'dataio_opus_nasse';
55  break;
56  end;
57 
58  break;
59  end;
60 
61  if isempty(classname)
62  % irerror('Could not detect type of file ''%s''', filename);
63  irwarning(sprintf('Could not detect type of file ''%s''', filename));
64  else
65  irverbose(sprintf('File type detected for file ''%s'': ''%s''', filename, classname), 1);
66  end;
67  fclose(id);
68 catch ME
69  fclose(id);
70  rethrow(ME);
71 end;
72 
73 
74 
75 
function irverbose(in s, in level)
function detect_file_type(in filename)
function irerror(in s)
Basic TXT loader/saver.
"Pirouette" TXT loader/saver
Definition: dataio_txt_pir.m:8
function strip_quotes(in s)
Dataset loader for OPUS "0" files.
Dataset loader/saver for mat files.
Definition: dataio_mat.m:5
function irwarning(in s)
IRootLab TXT loader/saver.