On Sat, Jul 7, 2012 at 12:14 PM, Peter Newman
<address@hidden> wrote:
I have the following code to read and process lines in a text file that includes a test to skip blank lines. I find that the program completes running after reaching the first empty line rather than skipping and reading the rest of the file, yet the result from fgetl is different when reading empty lines than reading EOF, so I expected this code to work.
1;
fid=fopen("data.txt");
l = fgetl (fid); % initialize with first line
lc = l(7:53); % take data from center
dblc = deblank (lc); % strip trailing spaces
cc = strsplit (dblc, ' '); % separate byte strings
ncc = length (cc);
hn = ncc/2;
for j = 1:hn; % join two bytes
ib(j) = strcat (cc(2*j-1),cc(2*j));
endfor
l = fgetl (fid);
% Loop through data file until we get a -1 indicating EOF
while (l~=(-1))
if ~isempty(l) % only process non-empty lines
lc = l(7:53); % take data from center
dblc = deblank (lc); % strip trailing spaces
cc = strsplit (dblc, ' '); % separate byte strings
ncc = length (cc);
hn = ncc/2;
clear cv % clear left overs from last line
clear bs
for j = 1:hn; % join two bytes
bs(j) = strcat (cc(2*j-1),cc(2*j));
endfor
ib = [ib, bs]; % append data from this line
endif
l = fgetl (fid); % get another line
end
Here is the contents of data.txt where line 1 starts with 0010:
0010 05 dc 63 f4 40 00 40 06 4f af c0 a8 00 1e c0 a8 address@hidden@.O.......
0020 00 0a 0e a4 ee 49 47 a8 ab ee 71 c2 12 55 80 10 .....IG...q..U..
0030 0b 68 a9 1b 00 00 01 01 08 0a ff ff c8 fa ff ff .h..............
0040 cc 41 44 02 00 00 01 00 40 02 52 02 address@hidden
0050 4e 02 50 02 4e 02 49 02 49 02 45 02 44 02 44 02 N.P.N.I.I.E.D.D.
0060 44 02 43 02 43 02 44 02 43 02 40 02 47 02 45 02 address@hidden
0070 45 02 4f 02 52 02 52 02 52 02 47 02 42 02 41 02 E.O.R.R.R.G.B.A.
0080 42 02 42 02 42 02 42 02 42 02 44 02 40 02 address@hidden
Any suggestions would be appreciated.