forked from arnodelorme/child_mind_arno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtxt.m
166 lines (156 loc) · 6.54 KB
/
loadtxt.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
% loadtxt() - load ascii text file into numeric or cell arrays
%
% Usage:
% >> array = loadtxt( filename, 'key', 'val' ...);
%
% Inputs:
% filename - name of the input file
%
% Optional inputs
% 'skipline' - number of lines to skip {default:0}. If this number is
% negative the program will only skip non-empty lines
% (can be usefull for files transmitted from one platform
% to an other, as CR may be inserted at every lines).
% 'convert' - 'on' standard text conversion, see note 1
% 'off' no conversion, considers text only
% 'force' force conversion, NaN are returned
% for non-numeric inputs {default:'on'}
% 'delim' - ascii character for delimiters. {default:[9 32]
% i.e space and tab}. It is also possible to enter
% strings, Ex: [9 ' ' ','].
% 'blankcell' - ['on'|'off'] extract blank cells {default:'on'}
% 'verbose' - ['on'|'off'] {default:'on'}
% 'convertmethod' - ['str2double'|'str2num'] default is 'str2double'
% 'nlines' - [integer] number of lines to read {default: all file}
%
% Outputs:
% array - cell array. If the option 'force' is given, the function
% retrun a numeric array.
%
% Notes: 1) Since it uses cell arrays, the function can handle text input.
% The function reads each token and then try to convert it to a
% number. If the conversion is unsucessfull, the string itself
% is included in the array.
% 2) The function adds empty entries for rows that contains
% fewer columns than others.
%
% Author: Arnaud Delorme, CNL / Salk Institute, 29 March 2002
% Copyright (C) Arnaud Delorme, CNL / Salk Institute, 29 March 2002
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function array = loadtxt( filename, varargin );
if nargin < 1
help loadtxt;
return;
end;
if ~isempty(varargin)
try, g = struct(varargin{:});
catch, disp('Wrong syntax in function arguments'); return; end
else
g = [];
end
g = finputcheck( varargin, { 'convert' 'string' { 'on';'off';'force' } 'on';
'skipline' 'integer' [0 Inf] 0;
'verbose' 'string' { 'on';'off' } 'on';
'uniformdelim' 'string' { 'on';'off' } 'off';
'blankcell' 'string' { 'on';'off' } 'on';
'convertmethod' 'string' { 'str2double';'str2num' } 'str2double';
'delim' { 'integer';'string' } [] [9 32];
'nlines' 'integer' [] Inf });
if ischar(g), error(g); end
if strcmpi(g.blankcell, 'off'), g.uniformdelim = 'on'; end
g.convert = lower(g.convert);
g.verbose = lower(g.verbose);
g.delim = char(g.delim);
% open the file
% -------------
if exist(filename) ~=2, error( ['file ' filename ' not found'] ); end;
fid=fopen(filename,'r','ieee-le');
if fid<0, error( ['file ' filename ' found but error while opening file'] ); end;
index = 0;
while index < abs(g.skipline)
tmpline = fgetl(fid);
if g.skipline > 0 || ~isempty(tmpline)
index = index + 1;
end;
end; % skip lines ---------
inputline = fgetl(fid);
linenb = 1;
if strcmp(g.verbose, 'on'), fprintf('Reading file (lines): '); end
while isempty(inputline) || inputline(1)~=-1
colnb = 1;
if ~isempty(inputline)
tabFirstpos = 1;
% convert all delimiter to the first one
if strcmpi(g.uniformdelim, 'on')
for index = 2:length(g.delim)
inputline(find(inputline == g.delim(index))) = g.delim(1);
end
end
while ~isempty(deblank(inputline))
if strcmpi(g.blankcell,'off'), inputline = strtrim(inputline); end
if tabFirstpos && length(inputline) > 1 && all(inputline(1) ~= g.delim), tabFirstpos = 0; end
[tmp inputline tabFirstpos] = mystrtok(inputline, g.delim, tabFirstpos);
switch g.convert
case 'off', array{linenb, colnb} = tmp;
case 'on',
if strcmpi(g.convertmethod, 'str2double')
tmp2 = str2double(tmp);
if isnan( tmp2 ) , array{linenb, colnb} = tmp;
else array{linenb, colnb} = tmp2;
end
else
tmp2 = str2num(tmp);
if isempty( tmp2 ) , array{linenb, colnb} = tmp;
else array{linenb, colnb} = tmp2;
end
end
case 'force', array{linenb, colnb} = str2double(tmp);
end
colnb = colnb+1;
end
linenb = linenb +1;
end
inputline = fgetl(fid);
if linenb > g.nlines
inputline = -1;
end
if ~mod(linenb,10) && strcmp(g.verbose, 'on'), fprintf('%d ', linenb); end
end;
if strcmp(g.verbose, 'on'), fprintf('%d\n', linenb-1); end
if strcmp(g.convert, 'force'), array = [ array{:} ]; end
fclose(fid);
% problem strtok do not consider tabulation
% -----------------------------------------
function [str, strout, tabFirstpos] = mystrtok(strin, delim, tabFirstpos);
% remove extra spaces at the beginning
while any(strin(1) == delim) && strin(1) ~= 9 && strin(1) ~= ','
strin = strin(2:end);
end
% for tab and coma, consider empty cells
if length(strin) > 1 && any(strin(1) == delim)
if tabFirstpos || any(strin(2) == delim)
str = '';
strout = strin(2:end);
if strin(2) ~= 9 && strin(2) ~= ','
tabFirstpos = 0;
strout = strtrim(strout);
end
else
[str, strout] = strtok(strin, delim);
end
else
[str, strout] = strtok(strin, delim);
end