reading a text file
2 views (last 30 days)
I am trying to read a text file having 11 headerlines at the beginning and 30 rows of numberical data (in 4 different columns). I want to skip l0 lines and read every other line. The code is as such: clc filename='sample4.txt'; StartLine=11; %to skip the headers
fclose('all'); fid1 = fopen(filename); if fid1 == -1 disp(' ') disp('Operation was unsuccessful - Check the filename')
for k=1:StartLine-1 tline=fgetl(fid1); end
k=1; while ~feof(fid1)
for i = 1:10
d = fgets(fid1);
if isempty(d)|| ~ischar(line), break,end
% d = fgetl(fidl);
numline(1:4) = str2num(d);
Z=numline(2);% 2nd column
Y=numline(3);% 3nd column
X=numline(4);% 4th column
%tline = fgetl(fidl);
Now my problem is that when I run the code I get this message: Which is as I believe, is happening from the loop. ??? Error using ==> str2num Requires string or character array input.
Error in ==> G:\Simulation\trail_RLE.m On line 31 ==> numline(1:4) = str2num(d);
Can someone help me please show me how to terminate the loop when the code reaches the end of the file??
Walter Roberson on 16 May 2012
fgets() does not return empty at end of file: it returns -1.
Your existing code does not catch that because the ischar() is being applied to "line" not to "d". (I don't see where "line" is defined?)