| MATLAB Function Reference | ![]() |
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
exist item exist itemkinda = exist('item',...)
Description
exist item
returns the status of the variable or file, item:
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 returns logical true (kind
1), if an item of the specified kind is found; otherwise, it returns 0. The kind argument may be one of the following:
|
Checks only for variables. |
|
Checks only for built-in functions. |
|
Checks only for files or directories. |
|
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,
a = 5.83;
c = 'teststring';
ismember({'a','b','c'},who)
ans =
1 0 1
Examples
This example uses exist to check whether a MATLAB function is a built-in or a file:
type = exist('plot')
type =
5
In the example below, exist returns 8 on the Java class, Welcome, and returns 2 on the Java class file, Welcome.class.
exist Welcome
ans =
8
exist javaclasses/Welcome.class
ans =
2
See Also
dir, help, lookfor, partialpath, what, which, who
| evalin | exit | ![]() |