reading a text file

4 views (last 30 days)
Azza Ahmed
Azza Ahmed on 16 May 2012
Hi,
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')
return
end
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
end
%
% d = fgetl(fidl);
numline(1:4) = str2num(d);
R=numline(1);
Z=numline(2);% 2nd column
Y=numline(3);% 3nd column
X=numline(4);% 4th column
%tline = fgetl(fidl);
end
fclose(fid1);
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??
AA

Answers (1)

Walter Roberson
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?)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!