~feof doesnt woek properly, how to fix the problem.

조회 수: 12 (최근 30일)
Reza S
Reza S 2015년 4월 29일
댓글: Reza S 2015년 5월 8일
Hello,
I had a code in Matlab 2011 to read some file and it was working well. I upgraded to Matlab 2014 and now feof doesn't recognize the end of the file and remains in the while loop as below. What I should do now?
while (~feof(fileID))
[data,position] = textscan(fileID,'%f',82,'delimiter', '\t\t');
real_data{counter,:} = data;
counter = counter + 1;
end
  댓글 수: 2
Guillaume
Guillaume 2015년 4월 29일
Can you attach a file that replicate the problem?
Very Determined
Very Determined 2015년 5월 3일
편집: Very Determined 2015년 5월 3일
Hello,
I have attached one of the files. I had to change the extension to .txt (from .log) as it was not allowed by this site. I have 18 files of the same type in a folder. Thanks for the help.

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

채택된 답변

Stephen23
Stephen23 2015년 5월 4일
편집: Stephen23 2015년 5월 4일
Using textscan inside of a while-loop is a waste of textscan's ability to read the whole file at once, and hacks like this never work well, as you have discovered. Observe that the file is very nicely structured and lends itself to using some simple options that allow us to read the whole file in just a few lines, without any loops. This code also adjusts automatically for the number of columns. Obviously it reads just one file, so you will need that for-loop to read all of the files:
fid = fopen('C0-1_CP40_MP10_R1.txt','rt');
date_time = textscan(fid,'Date%sTime%s','HeaderLines',1);
col_hdrs = regexp(fgetl(fid),'\t+','split');
tkn = repmat('%f',1,numel(col_hdrs));
data = cell2mat(textscan(fid,tkn,'delimiter', '\t', 'MultipleDelimsAsOne',true));
fclose(fid);
and we can view the data in the command window (viewing just the first four columns to save space):
>> col_hdrs(1:4)
ans =
'Time (s)' 'Pump speed [Hz]' 'Mixer speed [Hz]' [1x21 char]
>> data(:,1:4)
ans =
0.55 40 19.8 0.29203
2.55 40 19.8 0.29266
4.55 40 19.8 0.29364
6.56 40 19.8 0.29412
... lots more rows here!
58.55 40 19.8 0.28804
60.55 40 19.8 0.29222
62.56 40 19.8 0.29211
  댓글 수: 3
Stephen23
Stephen23 2015년 5월 5일
편집: Stephen23 2015년 5월 5일
The best way to "fix the issue" would be to replace that buggy hack-code with something more reliable, such as what I gave in my answer. The time that you spend replacing some hack-code with something more robust is time well spent, and you will not regret it. And it really is hack-code, don't try to believe otherwise:
  • it does not work
  • it probably has different behavior depending on the file encoding
  • it is a totally non-general solution
  • it depends on hard-coded magic numbers
  • concatenating arrays inside loops without array preallocation
  • slow, repeated calling of textscan in a loop... why bother, when textscan was written to read the whole file at once?
Don't get too emotionally attached to your code.
And if that code is really is repeated many times, then it is time to think about creating a helper-function to do this operation for you: then you would only need to fix it in one location, instead of many, and maintain one version. This is exactly what functions are for!
Reza S
Reza S 2015년 5월 8일
Thank you so much for detail explaining. I replaced the codes as suggested. They work perfect (as expected) and I am happy for what I did. Thanks again.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 5월 3일
Did you call fopen() before you called feof()? I suspect not. At least you didn't put it in your code.
  댓글 수: 3
Image Analyst
Image Analyst 2015년 5월 3일
I never use textscan() but when I've seen people use it they don't loop over lines but suck up the whole file in one call. Maybe Star will know - he uses it more than me.
Very Determined
Very Determined 2015년 5월 3일
편집: Very Determined 2015년 5월 3일
Thanks. Looking forward to here from Star.

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

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by