MATLAB Function Reference    
exist

Check if a variable or file exists

Graphical Interface

As an alternative to the exist function, use the Workspace browser. To open it, select Workspace from the View menu in the MATLAB desktop.

Syntax

Description

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

0
If item does not exist.
1
If the variable item exists in the workspace.
2
If item is an M-file or a file of unknown type.
3
If item is a MEX-file on your MATLAB search path.
4
If item is an MDL-file on your MATLAB search path.
5
If item is a built-in MATLAB function.
6
If item is a P-file on your MATLAB search path.
7
If item is a directory.
8
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 logical true (1), if an item of the specified kind is found; otherwise, it returns 0. The kind argument may be one of the following:

var
Checks only for variables.
builtin
Checks only for built-in functions.
file
Checks only for files or directories.
dir
Checks only for directories.
class
Checks only for Java classes.

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

Remarks

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

Examples

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

plot is a built-in function.

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

See Also

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


 evalin exit