reading a text file
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Walter Roberson
2012년 5월 16일
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?)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!