matlab quickie: test if text file is empty
Simple question: I am opening a file in matlab 7.x, and I want to test if it is empty before reading it开发者_开发知识库. What's the best way to do this?
Taking some knowledge from this previous question, I would do the following
s = dir('c:\somefile.txt');
if s.bytes == 0
% empty file
else
% open the file and read it
end;
I assumed by empty that you meant that there is really nothing in the file including new line characters. If by empty you mean only new line characters, then you should go ahead with your solution.
got it:
fid = fopen(fil);
if all(fgetl(fid) == -1)
% file is empty
else
fseek(fid,0,-1); % rewind it
end
This is the cleanest way I can think of:
if fseek(fileID, 1, 'bof') == -1
% empty file
else
rewind(fileID)
% ready to read
end
精彩评论