Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

exist - Check existence of variable, function, directory, or class

Graphical Interface

As an alternative to the exist function, use the Workspace Browser or the What Is the Current Folder Browser?

Syntax

exist name
exist name kind
A = exist('name','kind')

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 exists as 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 exists as a MEX- or DLL-file on your MATLAB search path.

4

If name exists as 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 class. (exist returns 0 for Java classes if you start MATLAB with the -nojvm option.)

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

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 specifies a filename, that filename may include an extension to preclude conflicting with other similar filenames. For example, exist('file.ext').

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

Class

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

Remarks

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'])

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 function or a file:

type = exist('plot')
type =
     5

This indicates that plot is a built-in function.

Run exist on a class directory and then on the constructor within that directory:

exist('@portfolio')
ans =
     7               % @portfolio is a directory

exist('@portfolio\portfolio')
ans =
     2               % portfolio is an M-file

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

See Also

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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS