File tree 1 file changed +18
-2
lines changed
1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 1
1
function varargout = csvimport( fileName , varargin )
2
2
% CSVIMPORT reads the specified CSV file and stores the contents in a cell array or matrix
3
+ % edited by Stijn Goossens to bump less frequent against memory issues
3
4
%
4
5
% The file can contain any combination of text & numeric values. Output data format will vary
5
6
% depending on the exact composition of the file data.
183
184
184
185
% Read first line and determine number of columns in data
185
186
rowData = fgetl( fid );
187
+ templine= rowData ; % added by goosst
186
188
rowData = regexp( rowData , p .delimiter , ' split' );
187
189
nCols = numel( rowData );
188
190
229
231
error( ' csvimport:FileQueryError' , ' FTELL on file ''%s'' failed.\n Error Message: %s ' , ...
230
232
fileName , msg ) ;
231
233
end
232
- data = fread( fid );
233
- nLines = numel( find( data == sprintf( ' \n ' ) ) ) + 1 ;
234
+
235
+ % less memory consuming way to get number of lines
236
+
237
+ % original: fread gives me a lot of out of memory complaints when reading larger csv files
238
+ % data = fread( fid);
239
+ % nLines = numel( find( data == sprintf( '\n' ) ) ) + 1;
240
+
241
+ % new addition:
242
+ lines= 1 ;
243
+ while ischar(templine )
244
+ templine = fgetl(fid );
245
+ lines= lines + 1 ;
246
+ end
247
+ nLines= lines - 1 ;
248
+
249
+
234
250
% Reposition file position indicator to beginning of second line
235
251
if fseek( fid , pos , ' bof' ) ~= 0
236
252
msg = ferror( fid );
You can’t perform that action at this time.
0 commit comments