How to Check the Existence of a File in MATLAB
-
Check the Existence of a File Using the
exist()
Function in MATLAB -
Check the Existence of a File Using the
dir()
Function in MATLAB
This tutorial will discuss checking the existence of a file in a directory using the exist()
and dir()
function in MATLAB.
Check the Existence of a File Using the exist()
Function in MATLAB
We can check the existence of a file in a directory using the exist()
function of MATLAB. The exist()
function has there kinds of syntaxes.
The first syntax of the exist()
function is given below.
exist file_name
In the above code, we have to pass the file or folder name in place of the file_name
. In the case of a folder, we will define the folder name; and in the case of a file, we have to define its extension.
The exist()
function will return 0
if the file does not exist or MATLAB does not have permission to the specific folder. The exist()
function will search for the file or folder in the current directory used by MATLAB and its built-in functions and classes directory.
For example, if we have created a MATLAB .m
file in a specific folder, the exist()
will only search for the file in that folder and the built-in function and classes directory of MATLAB.
The exist()
function will return 1
if the file name is a name of a variable in the workspace of MATLAB. It will return 2
if the file name exists with an extension .mlx
or .m
.
The function will return 3
if the file is a MEX file in the current directory. The function will return 4
if the file name is a Simulink model file in the current directory.
It will return 5
if the file name is a built-in function of MATLAB. The function will return 6
if the file name is a P-code file in the current directory.
It will return 7
if the file name is a folder. The function will return 8
if the file name is the name of a class in MATLAB.
It searches for the specified file in precedence order, and if a file is found, it will return the result and stop the search. Check this link for more details about the function precedence order.
Example:
clc
clear
exist Videos
Output:
ans =
7
The function returned 7
, which means the specified filename is a folder in the current directory.
The exist()
function also has two other syntaxes given below.
exist file_name searchType
type = exist('file_name')
The searchType
is used as an optional argument to search for a specific file type in the above code. It is useful when we only want to search for a particular file like a built-in function.
In the second syntax of the exist()
function, we can save the returned value in a variable. For example, let us check a file name in the built-in function directory of MATLAB.
clc
clear
exist exist builtin
type = exist('exist','builtin')
Output:
ans =
5
type =
5
In the above code, we used two syntaxes of the exist()
function, and we searched for the existing file name inside the built-in function directory of Matlab. The output is 5 because the existing file name is the name of a built-in function in Matlab.
Check this link for more details about the exist()
function.
Check the Existence of a File Using the dir()
Function in MATLAB
We can check the existence of a file in a directory using the dir()
function of MATLAB. The syntax of the dir()
function is given below.
dir('file_name')
In the above code, the dir()
function returns a struct field containing five variables that have information about the given file name. If the file name does not exist, the five variables will be empty.
We can check the size of the output of the dir()
function to check if the given file exists in the current directory or not. For example, if the file exists, the output size of the dir()
function will be 1
, and in the case of a folder name, the size will be 5
.
Example:
clc
clear
d = dir('animal.jpg')
s = size(dir('animal.jpg'),1)
Output:
d =
struct with fields:
name: 'animal.jpg'
folder: 'C:\Users\ammar'
date: '09-Mar-2022 13:20:18'
bytes: 48580
isdir: 0
datenum: 7.3859e+05
s =
1
In the above code, we don’t have to use the d
variable line to check the file’s existence, but it can check other files’ details. In the above output, the variable s
is 1
, which means the file is found in the current directory of MATLAB.
Note that the dir()
function will only search for the file in the current directory used by MATLAB. If we pass a folder name, the size()
function will return 5
if the folder is found and 0
if not found.
Check this link for more details about the dir()
function.