MATLAB Function Reference    
exist

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.

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 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:

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

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 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