MATLAB Function Reference Previous page   Next Page
exist

Check if variables or functions are defined

Graphical Interface

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

Syntax

Description

exist('name') returns the status of name:

0
If name does not exist.
1
If name is a variable in the workspace.
2
If name is an M-file on your MATLAB search path. It also returns 2 when name is the full pathname to a file or the name of an ordinary file on your MATLAB search path.
3
If name is a MEX- or DLL-file on your MATLAB search path.
4
If name is an MDL-file on your MATLAB search path.
5
If name is a built-in MATLAB function.
6
If name is a P-file on your MATLAB search path.
7
If name is a directory.
8
If name is a Java class. (exist returns 0 if you start MATLAB with the -nojvm option.)

exist name kind returns the status of name for the specified kind. If name 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.

If name belongs to more than one category (e.g., if there are both an M-file and variable of the given name) and you do not specify a kind argument, exist returns one value according to the order of evaluation shown in the table below. For example, if name matches both a directory and M-file name, exist returns 7, identifying it as a directory.

Order of Evaluation
Return Value
Type of Entity
1
1
Variable
2
5
Built-in
3
7
Directory
4
3
MEX or DLL-file
5
4
MDL-file
6
6
P-file
7
2
M-file
8
8
Java class

A = exist('name','kind') is the function form of the syntax.

Remarks

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

If name specifies a filename, MATLAB attempts to locate the file, examines the filename extension, and determines the value to return based on the extension alone. MATLAB does not examine the contents or internal structure of the file.

You can specify a partial path to a directory or file. A partial pathname is a pathname relative to the MATLAB path that contains only the trailing one or more components of the full pathname. For example, both of the following commands return 2, identifying mkdir.m as an M-file. The first uses a partial pathname:

If a file or directory is not on the search path, then name must specify either a full pathname, a partial pathname relative to MATLABPATH, a partial pathname relative to your current directory, or the file or directory must reside in your current working directory.

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

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

assignin, computer, dir, evalin, help, inmem, isempty, lookfor, mfilename, partialpath, what, which, who


Previous page  evalin exit Next page

© 1994-2005 The MathWorks, Inc.