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

10 ビュー (過去 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 Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by