forked from gremau/NMEG_FluxProc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_delimiter.m
40 lines (35 loc) · 1.06 KB
/
detect_delimiter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function delim = detect_delimiter( fpath )
% DETECT_DELIMITER - makes a best guess between space, tab, comma, and
% semicolon at the delimiter used in a text file.
%
% Chooses the most frequently occuring delimiter among space, tab, comma, and
% semicolon.
%
% USAGE
% delim = detect_delimiter( fpath );
%
% INPUTS
% fpath: full path to file to analyze
%
% OUTPUTS
% delim: character; the best-guess delimiter
%
% author: Timothy W. Hilton, UNM, Oct 2011
[n, lines] = parse_file_lines(fpath);
n_space = sum(cellfun(@(x) length(regexp(x, ' ', 'start')), lines));
n_tab = sum(cellfun(@(x) length(regexp(x, '\t', 'start')), lines));
n_comma = sum(cellfun(@(x) length(regexp(x, ',', 'start')), lines));
n_semicolon = sum(cellfun(@(x) length(regexp(x, ';', 'start')), lines));
[m, idx] = max( [ n_space, n_tab, n_comma, n_semicolon ] );
switch( idx )
case 1
delim = ' ';
case 2
delim = '\t';
case 3
delim = ',';
case 4
delim = ';';
otherwise
delim = NaN;
end