| MATLAB Function Reference | ![]() |
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:
exist name returns the status of kind
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:
|
Checks only for built-in functions. |
class |
Checks only for Java classes. |
|
Checks only for directories. |
|
Checks only for files or directories. |
|
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',' is the function form of the syntax. kind')
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
| evalin | exit | ![]() |
© 1994-2005 The MathWorks, Inc.