|
% Steve Amphlett wrote:
% > Matthew Crema wrote:
%
% > <snip, line-by-line reading is slow c.f. ML's <load> ...
%
% >>What is the load command doing that I am not?
%
% > I think it's probably down to buffering. If you read a file
% > line-by-line using C's fgets() function in a loop, the performance
is
% > much better than doing the equivalent in ML.
%
% > Based on postings to this NG (and my limited experience of using
ML's
% > file reading functions), a bulk read of the whole file, followed
by
% > string parsing of the resulting character array is a good
approach.
% > A MEX file is better, and normally quicker than <load> too.
%
% Thanks Steve!
%
% tic
% fid = fopen('mydata.dat','r');
% header = fgetl(fid); % Chop off the header
% data = fscanf(fid,'%f',[4,inf]); % 4x72000 matrix
% data=data'; % Not sure why I have to do this
% fclose(fid)
% toc
%
% 2.92 seconds! (Compare to load which takes 3.795 seconds)
%
% -Matt
from above:
% data=data'; % Not sure why I have to do this
from another post:
c=c.';
txt=sprintf([repmat('%s\t',1,size(c,1)),'\n'],c{:});
I like the results of this second example, but I never would have
thought to write that on my own.
I generally feel that many of the file and string functions (textscan,
etc.) transpose my string data in unexpected ways. I would appreciate
it if anyone would kindly explain the general rule so I can figure out
how to write the proper code without guessing. Thanks.
|