-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvreadh.m
executable file
·42 lines (35 loc) · 1.01 KB
/
csvreadh.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
function [h,m] = csvreadh( filename, delim )
%CSVREADH Read a comma separated value file with header.
% [H,M] = CSVREADH('FILENAME') reads a comma separated value formatted file
% FILENAME. The result data is returned in M, the header in H.
% The file can only contain numeric values as data and a string for
% the header.
% Validate input args
if nargin==0
error(nargchk(1,1,nargin,'struct'));
end
% Get Filename
if ~ischar(filename)
error('csvreadh:FileNameMustBeString', ...
'Filename must be a string.');
end
% Make sure file exists
if exist(filename,'file') ~= 2
error('csvreadh:FileNotFound',...
'File not found.');
end
if nargin==1
delim = ',';
end
% open input file
file = fopen( filename );
line = fgetl( file );
h = regexp( line, delim, 'split' );
m = [];
% this is not quick for sure, but works
while 1
line = fgetl( file );
if ~ischar(line), break, end
m = [m; str2double(regexp( line, ',', 'split' ))];
end
fclose(file);