~feof doesnt woek properly, how to fix the problem.
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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
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!
更多回答(1 个)
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!