Check existence of variable, script, function, folder, or class
exist name searchType
A = exist(___)
the type of
name as a number. This list describes
the type associated with each value:
name does not exist.
name is a variable in
name is a file with
name is the name of a file with a non-registered
file extension (
name is a MEX-file on
your MATLAB® search path.
name is a Simulink® model or library file on your MATLAB search
name is a built-in MATLAB function.
This does not include classes.
name is a P-code file
on your MATLAB search path.
name is a folder.
name is a class. (
exist returns 0 for Java classes
if you start MATLAB with the
MATLAB searches starting at the top of the search
path, and moving down until a result is found or the last folder on
the path is reached. If more than one
in a folder, MATLAB displays the first instance of
according to the Function Precedence Order. Folders are an exception
to the function precedence rules. They have precedence over all types
except for variables.
For example, if
name matches both a file
.m extension and a P-code file, then
6, identifying it as a P-code file. If
both a variable and a P-code file, exists returns 1, identifying it
as a variable. If
name matches both a folder and
a MATLAB function,
identifying it as a folder.
Create a variable named
testresults, and then confirm its existence in the workspace.
testresults = magic(5); exist testresults
ans = 1
A variable named
testresults exists in the workspace.
Create the folder
myfolder, and then check its existence as a folder.
mkdir myfolder; exist myfolder dir
ans = 7
If you specify the type as
file, MATLAB® searches for both files and folders, therefore returning the same result.
exist myfolder file
ans = 7
Check whether the
plot function is a built-in function or a file.
A = exist('plot')
A = 5
This indicates that
plot is a built-in MATLAB function.
name— name of variable, script, function, folder, or class
Name of variable, script, function, folder, or class, specified
as a character vector or string.
name can include
a partial path, but must be in a folder directly on the search path
(subfolders are not searched), or in the current folder. Otherwise,
include a full path.
name specifies a file with a non-registered
file extension (
include the extension. You can also include an extension to prevent
conflict with other similar file names. For example,
On Windows® systems,
name is not case
sensitive. On UNIX® systems,
name is case sensitive
for files with a non-registered file extension (
For files with a registered file extension,
not case sensitive, but preference is given to results with an exact
MATLAB does not examine the contents or internal structure of a file and relies solely on the file extension for classification.
searchType— Type of results to search for
Type of results to search for, specified as one of these values:
|searchType||Description||Possible Return Values|
Checks only for built-in functions.
Checks only for classes.
Checks only for folders.
Checks only for files or folders.
2, 3, 4, 6, 7, 0
Checks only for variables.