Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab quickie: test if text file is empty

Tags:

file-io

matlab

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?

like image 552
johndashen Avatar asked Feb 25 '10 23:02

johndashen


People also ask

How to check if a txt file is empty in MATLAB?

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.

How do you check if a file is in a folder Matlab?

Description. result = isfolder( folderName ) returns 1 if folderName is a folder located on the specified path or in the current folder. Otherwise, isfolder returns 0.

How do I read a filename in Matlab?

Accepted Answer filePattern = fullfile('./', '*. m'); files = dir(filePattern);


1 Answers

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.

like image 75
Justin Peel Avatar answered Sep 30 '22 12:09

Justin Peel