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

searching for new line character with strfind

318 views
Skip to first unread message

James Roberts

unread,
Jan 28, 2011, 2:33:04 PM1/28/11
to
Hi all,

I am trying to read data from a formatted text file. the lines are such that each line begins with a one- or two-character marking of the data type in that line. The current method i use works, but takes an unreasonably long time - looping through each line of the file and reading the data line-by-line.

I would like to read the data all at once with:
file_text = fread(fid, inf, 'uint8=>char')';
and then use strfind to get the indices of the new line characters. unfortunately my calls to strfind are only returning empty arrays:
strfind(file_text,'\n')
ans =
[]

what am i doing wrong?

Matt Fig

unread,
Jan 28, 2011, 2:50:05 PM1/28/11
to
Could you post a few example lines of the file?

ImageAnalyst

unread,
Jan 28, 2011, 2:57:25 PM1/28/11
to
On Jan 28, 2:33 pm, "James Roberts" <jamescrobe...@hotmail.com> wrote:
[snip]

> strfind(file_text,'\n')
> ans =
>      []
>
> what am i doing wrong?

===================================
Try using strtok() instead.

James Roberts

unread,
Jan 28, 2011, 3:42:04 PM1/28/11
to
"Matt Fig" wrote in message <ihv6ld$n41$1...@fred.mathworks.com>...

> Could you post a few example lines of the file?

sure. here are the first 6 lines:

a -0.675 1.850 3.000
a 0.915 1.930 4.111
b 2.485 0.470 2.000
a 2.485 -1.030 5.901
b 1.605 -1.890 10.700
b -0.745 -0.654 0.500

if you use fopen to open a *.txt file with just these lines and then use "data = fread(fid, inf, 'uint8=>char')';" to read in the data, then data(23) and data(24) seem to be newline characters (don't know why there are two there). i've copied and pasted the manual check i did below.

>>datachar = data(23);
>> disp(['x',datachar,'x'])
x
x
>> datachar = data(24);
>> disp(['x',datachar,'x'])
x
x

but i still get:
>> strfind(data,'\n')
ans =
[]

Jan Simon

unread,
Jan 28, 2011, 3:53:03 PM1/28/11
to
Dear James,

> >> strfind(data,'\n')

This searches for backslash and 'n'! So either use:
strfind(data, sprintf('\n'))
or
strfind(data, char(10));
And perhaps the file has DOS line breaks, then use:
strfind(data, char([13, 10]));

Kind regards, Jan

Think two, count blue.

unread,
Jan 28, 2011, 3:56:36 PM1/28/11
to

Are you fopen()'ing the file with 'r' or 'rt' ? If you open with 'r' and you
are using MS Windows, then the end of line sequence is a character pair, \r\n
(carriage-return newline, ^M^J, 13 10). I believe opening with 'rt' should
cause the pair to be replaced with \n

Another problem you are facing is that \n is only special in formats, not in
general character constants. Your search was for the literal pair of
characters \ followed by n .

strfind(file_text, sprintf('\n'))

James Roberts

unread,
Jan 28, 2011, 4:12:04 PM1/28/11
to
"Jan Simon" wrote in message <ihvabf$ntp$1...@fred.mathworks.com>...

thank you! that worked beautifully!

James Roberts

unread,
Jan 28, 2011, 4:12:04 PM1/28/11
to
"Think two, count blue." <robe...@hushmail.com> wrote in message <ihvai8$vir$1...@nrc-news.nrc.ca>...

thank you! that worked beautifully!

0 new messages