| MATLAB Function Reference | ![]() |
As an alternative to the exist function, use the Workspace Browser or the Current Directory Browser.
exist name
exist name kind
A = exist('name','kind')
exist name returns the status of name:
If name does not exist. | |
If name is a variable in the workspace. | |
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. | |
If name is a MEX- or DLL-file on your MATLAB search path. | |
If name is an MDL-file on your MATLAB search path. | |
If name is a built-in MATLAB function. | |
If name is a P-file on your MATLAB search path. | |
If name is a directory. | |
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:
Checks only for built-in functions. | |
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','kind') is the function form of the syntax.
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:
exist('matlab/general/mkdir.m')
exist([matlabroot '/toolbox/matlab/general/mkdir.m'])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.
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 1This example uses exist to check whether a MATLAB function is a built-in function or a file:
type = exist('plot')
type =
5This 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:
exist Welcome
ans =
8
exist javaclasses/Welcome.class
ans =
2indicates 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:
exist('testresults','var')
ans =
1
exist('testresults','dir')
ans =
7assignin, computer, dir, evalin, help, inmem, isfield, isempty, lookfor, mfilename, partialpath, what, which, who
![]() | exifread | exit | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |