MATLAB Function Reference    

Check if a variable or file exists

Graphical Interface

As an alternative to the exist function, use the Workspace browser or the Current Directory Browser.



exist item returns the status of the variable or file, item:

If item does not exist.
If the variable item exists in the workspace.
If item is an M-file or a file of unknown type.
If item is a MEX-file on your MATLAB search path.
If item is an MDL-file on your MATLAB search path.
If item is a built-in MATLAB function.
If item is a P-file on your MATLAB search path.
If item is a directory.
If item is a Java class.

If item specifies a filename, that filename may include an extension to preclude conflicting with other similar filenames. For example, exist('file.ext').

MEX, MDL, and P-files must be on the MATLAB search path for exist to return the values shown above. If item is found, but is not on the MATLAB search path, exist('item') returns 2, because it considers item to be an unknown file type.

Any other file type or directory specified by item is not required to be on the MATLAB search path to be recognized by exist. If the file or directory is not on the search path, then item must specify either a full pathname, a partial pathname relative to MATLABPATH, or a partial pathname relative to your current directory.

If item is a Java class, then exist('item') returns an 8. However, if item is a Java class file, then exist('item') returns a 2.

exist item kind returns the status of item for the specified kind. If item of type kind does not exist, it returns 0. The kind argument may be one of the following:

Checks only for built-in functions.
Checks only for Java classes.
Checks only for directories.
Checks only for files or directories.
Checks only for variables.

a = exist('item',...) returns the status of the variable or file in variable a.


To check for the existence of more than one variable, use the ismember function. For example,


This example uses exist to check whether a MATLAB function is a built-in function or a file:

This indicates that plot is a built-in function.

In the following example, exist returns 8 on the Java class, Welcome, and returns 2 on the Java class file, Welcome.class.

indicates there is a Java class Welcome and a Java class file Welcome.class.

The following example indicates that testresults is both a variable in the workspace and a directory on the search path:

See Also

dir, help, lookfor, partialpath, what, which, who

  events (COM) exit