forked from lawrennd/ndlutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtableRead.m
51 lines (46 loc) · 1.26 KB
/
tableRead.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
41
42
43
44
45
46
47
48
49
50
51
function [columnNames, data] = tableRead(fileName, separator)
% TABLEREAD Read in data which has column titles in the first line and separated values in each other line.
% FORMAT
% DESC reads in data from a file that has column titles in the first line and
% separated values in every other line.
% ARG fileName : file name in which the data is stored.
% ARG separator : separator between the columns (default ',').
% RETURN columnNames : the names of the columns taken from the
% first line.
% RETURN data : the data, taken from the remaining lines.
%
% SEEALSO : fopen, fgetl
%
% COPYRIGHT : Neil D. Lawrence, 2004
% NDLUTIL
if nargin < 2
separator = ',';
end
fid = fopen(fileName);
i = 0;
while 1
i = i + 1;
lin=fgetl(fid);
if ~ischar(lin), break, end
end
numLines = i;
fid = fopen(fileName);
lin = fgetl(fid);
columnNames = stringSplit(lin, separator);
numCol = length(columnNames);
i = 0;
data = zeros(numLines - 1, numCol);
while 1
i = i+1;
lin=fgetl(fid);
if ~ischar(lin), break, end
split = stringSplit(lin, separator);
if length(split) ~= numCol
error(['Error at line ' num2str(i) ' of file ' fileName ': wrong ' ...
'number of columns'])
end
for j = 1:length(split);
data(i, j) = num2str(split{j});
end
end
fclose(fid);