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?
===================================
Try using strtok() instead.
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 =
[]
> >> 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
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'))
thank you! that worked beautifully!
thank you! that worked beautifully!