-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsvread.m
85 lines (85 loc) · 2.88 KB
/
tsvread.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function varargout = tsvread( varargin )
%[data, header, raw] = tsvread( file ) reads in text file with tab-seperated variables. default value for data is nan.
%alternative input/output option is suppluying header strings
%[col1, col2, col3, ..., header, raw] = tsvread( file, header1, header2, header3, ... )
%header is the first row (assumed to have header names) and raw is the imported text
%if a vector is supplied, this specifies the number rows to be imported.
%examples:
%[col1, col2,col3,header,raw] = tsvread( 'example.tsv', 'header1', 'header2', 'header3', 1:5 )
%will import data from example.tsv, and cols corresponding to header1,
%header2, header3, cols 1 t0 5.
%if no outputs are requested, then a portion of the rquested table is
%displayed -- good idea to see how the import and header requests are
%working!
%If there is a tsv file in local directory, then just running "tsvread" at
%the command line will read the newest tsv file and display first ten rows to screen.
%sak 9/2/11
if nargin == 0
disp( 'importing most recent tsv file in local directory' );
d = dir( '*.tsv' );
[~,i] = sort( [d.datenum], 'descend' );
fprintf( 'tsvread( ''%s'', 1:10 )\n', d(i(1)).name );
eval( sprintf( 'tsvread( ''%s'', 1:10 )', d(i(1)).name ) );
return;
end;
fid = fopen( varargin{1}, 'r' );
if nargout == 0
fprintf( 'loading %s, and displaying sideways\n', varargin{1} );
end
varargin(1) = [];
stuff = textscan( fid, '%s', 'delimiter', '\n');
stuff = stuff{1};
fclose(fid);
numrows = 1:size(stuff,1);
ind = cellfun( 'isclass', varargin, 'double' );
if any( ind )
numrows = varargin{ind};
varargin(ind) = [];
if numel(numrows) == 1
numrows = 1:min(numrows, size( stuff, 1) );
end
end
numrows = intersect( numrows, 1:size( stuff, 1 ) );
header = regexprep( regexp( stuff{1}, '[^\t]*\t', 'match' ), '\t', '' );
raw = repmat( {}, numel(numrows), numel(header) );
for i=numrows
stuff{i}(end+1) = 9;
tmp = regexprep( regexp( stuff{i}, '[^\t]*\t', 'match' ), '\t', '');
raw(i,1:numel(tmp)) = tmp;
end
header = raw(1,:);
data = nan(size(raw));
for i=numrows
for j=1:size( raw, 2 )
if ~isempty( raw{i,j} )
[a, count, errmsg] = sscanf( raw{i,j}, '%f' );
if ~isempty( a )
data(i,j) = a;
end
end
end
end
%%
if numel( varargin ) == 0
j=1:size(data, 2);
else
j = [];
for i=1:numel(varargin)
j = [j, find( strncmp( header, varargin{i}, numel(varargin{i})) )];
end
end
if nargout == 0
disp( [ strvcat( header(j)), num2str( data(numrows,j)' ) ] );
return;
end
if numel(varargin)==0
varargout = {data, header, raw};
varargout = varargout(1:nargout);
return;
end
varargout = {};
for i=j
varargout{end+1} = data( :, i );
end
varargout{end+1} = header(:,j);
varargout{end+1} = raw(:,j);