IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
get_no_cols_deli.m
Go to the documentation of this file.
1 %>@ingroup ioio string
2 %>@file
3 %>@brief
4 %> @brief Determines number of columns and delimiter from a CSV file
5 %>
6 %> Delimiter is determined by maximum probability among a list of the most common CSV delimiters
7 %
8 %> @param filename
9 %> @retval [no_cols, deli]
10 function [no_cols, deli] = get_no_cols_deli(filename)
11 % Opens for the first time to determine the number of columns
12 fid = fopen(filename);
13 if fid == -1
14  irerror(sprintf('File not found: %s !', filename));
15 end;
16 
17 s = fgets(fid);
18 
19 % Tries to figure out which is the delimiter being used
20 delimiters = sprintf(';,\t');
21 no_delimiters = length(delimiters);
22 no_colss = zeros(1, no_delimiters);
23 for i = 1:no_delimiters
24  no_colss(i) = sum(s == delimiters(i))+1;
25 end;
26 
27 [no_cols, index] = max(no_colss);
28 deli = delimiters(index);
29 fclose(fid);
function irerror(in s)
function get_no_cols_deli(in filename)