~feof doesnt woek properly, how to fix the problem.
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Reza S
el 29 de Abr. de 2015
Comentada: Reza S
el 8 de Mayo de 2015
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 comentarios
Very Determined
el 3 de Mayo de 2015
Editada: Very Determined
el 3 de Mayo de 2015
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.
Respuesta aceptada
Stephen23
el 4 de Mayo de 2015
Editada: Stephen23
el 4 de Mayo de 2015
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 comentarios
Stephen23
el 5 de Mayo de 2015
Editada: Stephen23
el 5 de Mayo de 2015
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!
Más respuestas (1)
Image Analyst
el 3 de Mayo de 2015
Did you call fopen() before you called feof()? I suspect not. At least you didn't put it in your code.
3 comentarios
Image Analyst
el 3 de Mayo de 2015
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
el 3 de Mayo de 2015
Editada: Very Determined
el 3 de Mayo de 2015
Thanks. Looking forward to here from Star.
Ver también
Categorías
Más información sobre Text Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!