HOWTO · MATLAB
How to Check the Existence of a File in MATLAB
Check whether a MATLAB file exists with exist() or inspect matching file details with dir().
On this page
Use exist() for a direct check, or dir() when you also need details about a match. Pass a file name or path explicitly when you need to control what MATLAB searches. The examples below create their own temporary files and use MATLAB-compatible syntax. Both were run with Octave; the output shown is from Octave, not MATLAB.
Check the Existence of a File Using the exist() Function in MATLAB
exist() can check for a file or folder, but an unqualified name can also match another MATLAB item, such as a workspace variable or function. For a file-only check, specify the 'file' search type: a result of 2 indicates a matching file. For a folder check, specify 'dir'; a result of 7 indicates a matching folder. A result of 0 means no item of the requested type was found. MATLAB resolves relative names from its current folder and search path; use an absolute path to identify a particular location. See MathWorks’ exist reference and function precedence order for details.
This complete example creates a file and checks its full path. It prints the file-type code, which is 2 for the created file according to exist() semantics. The Octave run printed:
root = tempname();
mkdir(root);
filePath = fullfile(root, 'report.txt');
fid = fopen(filePath, 'w');
fprintf(fid, 'Monthly report\n');
fclose(fid);
fileType = exist(filePath, 'file');
fprintf('File type: %d\n', fileType);
delete(filePath);
rmdir(root);
File type: 2
The 'file' search type avoids treating a same-named variable or folder as the file you want. To check a folder, call exist(folderPath, 'dir') instead. The older command form, exist name, is convenient at the prompt, while the function form lets a script store and compare the result.
Check the Existence of a File Using the dir() Function in MATLAB
Use dir() when you want the matching entry’s details as well as a presence check. It returns a structure array; each match can include fields such as name, folder, date, bytes, and isdir. The field set may vary by MATLAB release, so do not rely on a fixed field count. isempty(matches) tells you whether the pattern matched anything, and matches(1).bytes provides the byte count for a file match. A folder result is a directory entry, not a fixed-size listing. Calling dir() on a folder path can list its contents, so inspect isdir when distinguishing a folder from a file. Relative paths use MATLAB’s current folder; a full path makes the target explicit. See MathWorks’ dir reference.
This separate example creates a file, asks dir() for that exact path, and prints its returned name and size. The Octave run printed:
root = tempname();
mkdir(root);
filePath = fullfile(root, 'animal.jpg');
fid = fopen(filePath, 'w');
fprintf(fid, 'image data');
fclose(fid);
matches = dir(filePath);
if isempty(matches)
fprintf('No match\n');
else
fprintf('Name: %s\n', matches(1).name);
fprintf('Bytes: %d\n', matches(1).bytes);
end
delete(filePath);
rmdir(root);
Name: animal.jpg
Bytes: 10
For existence alone, exist(path, 'file') is the simpler check. Choose dir() when the entry’s details or a wildcard listing are useful, and use the returned structure array rather than assuming the size of a folder listing identifies a match.