Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get textscan to *read* blank lines?

177 views
Skip to first unread message

Tamara Kolda

unread,
Aug 1, 2008, 7:21:01 PM8/1/08
to
I'm trying to read all the lines in a file, some of which
are entirely blank. I'd like to use the textscan command.
Here is the command I tried...

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?

Gabe

unread,
Aug 1, 2008, 7:50:04 PM8/1/08
to
I couldn't find the files I wanted to double check but I believe I've used fgetl and it was very fast. I have not used textscan before.

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

Donn Shull

unread,
Aug 1, 2008, 8:19:02 PM8/1/08
to
Gabe <gabe.s...@controltheorypro.com> wrote in message
<7519044.12176346349...@nitrogen.mathforum.o
rg>...

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'));

Doug Schwarz

unread,
Aug 1, 2008, 10:07:16 PM8/1/08
to
In article <g705ot$qts$1...@fred.mathworks.com>,
"Tamara Kolda" <tgk...@sandia.gov> wrote:


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.

Chris Troutner

unread,
Jan 14, 2011, 1:15:05 PM1/14/11
to
Yep, that works!

I tried:

raw = textscan(fid, '%s', 'delimiter', '\n')

and that reads in blank lines.

Cheers!

Chris Troutner

0 new messages