필터 지우기
필터 지우기

Question about fgetl function

조회 수: 3 (최근 30일)
Diego
Diego 2012년 11월 13일
Dear all,
I'm learning to use fgetl and had followed the example that appears in the documentation:
fid = fopen('SomeFile.xxx');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
The example works great, but I don't understand why I have to declare the tline again inside the loop.
If I don't do that it keeps looping for ever!
Can somebody explain me the reason?
Thanks,
Diego

채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 13일
When you use tline = fgetl(fid) before the loop, that does not define tline to be an active function that returns the next line every time it is referenced. Instead, that statement reads a line once and assigns the line to tline as a string. In order to advance to the next line, you need to call fgetl() again. tline = fgetl(fid) is not a declaration: it is an assignment in which a current value is saved to a variable; in order to update the value, the function fgetl() needs to be called again.
  댓글 수: 1
Diego
Diego 2012년 11월 13일
Thank you Walter.
Regards,
Diego

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by