C = textscan(fid,'%s','Delimiter','','BufSize',100000);
It is very fast but skips all the blank lines.
Instead, I am forced to use the following:
n = 0;
while 1
tline{n+1} = fgetl(fid);
if tline{n+1} == -1, break, end
n = n + 1;
end
This is very slow but gets all the blank lines.
Any advice?
The code you list
n = 0;
while 1
tline{n+1} = fgetl(fid);
if tline{n+1} == -1, break, end
n = n + 1;
end
is going to be slow because tline is getting resized adn reallocated in memory on every loop pass. To dramatically speed up this code tline needs to be initialized.
Since you are using tline as a cell array you would initialize tline in this manner
tline = cell(numLines, 1);
where numLines is the number of lines of text in your file.
Assuming you don't know the number of lines of text in your file you will need to determine the number bytes in the file (there's an easy to do this but I can't remember it right now - probably related to fscan or fid). You will also need an estimate of the number bytes per line.
Estimating the number of lines can be a little tricky. So I'll give you the easy way.
numLines = 1E6;
n = 0;
tline = cell(numLines, 1);
while 1
tline{n+1} = fgetl(fid);
if tline{n+1} == -1, break, end
n = n + 1;
if mod(n, numLines) == 1
tline = [tline ; cell(numLines, 1)]; % the brackets may need to be {} instead
end
end
% clean up tline
tline = tline(n, 1);
This extra code only has MATLAB resizing the cell array every 1E6 lines. This should speed up the code a lot.
Gabe
I have a website with MATLAB help, here it is
http://wikis.controltheorypro.com/index.php?title=Category:MATLAB
How about reading the whole file into a string and using
strread ie
fid = fopen(fName);
str = char(fread(fid)');
fclose(fid);
lines = strread(str, '%s', 'delimiter', sprintf('\n'));
You are very close.
Try
C = textscan(fid,'%s','Delimiter','\n','BufSize',100000);
and see if it does what you want.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
I tried:
raw = textscan(fid, '%s', 'delimiter', '\n')
and that reads in blank lines.
Cheers!
Chris Troutner