exist

Check existence of variable, function, folder, or class

Syntax

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

Description

exist name returns the status of name:

0

name does not exist.

1

name is a variable in the workspace.

2

One of the following is true:

  • name exists on your MATLAB® search path as a file with extension .m.

  • name is the name of an ordinary file on your MATLAB search path.

  • name is the full pathname to any file.

3

name exists as a MEX-file on your MATLAB search path.

4

name exists as a Simulink® model or library file on your MATLAB search path.

5

name is a built-in MATLAB function.

6

name is a P-file on your MATLAB search path.

7

name is a folder.

8

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 folder 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 folder, or the file or folder must reside in your current working folder.

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

file

Checks only for files or folders.

var

Checks only for variables.

If you do not specify a kind argument, and name belongs to more than one of these categories, exist returns one value according to the order of evaluation shown in the table below. For example, if name matches both a folder and a file that defines a MATLAB function, exist returns 7, identifying it as a folder.

Order of Evaluation

Return Value

Type of Entity

1

1

Variable

2

5

Built-in

3

7

Folder

4

3

MEX-file

5

6

P-file

6

2

MATLAB function

7

4

SLX or MDL-file

8

8

Class

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

Examples

expand all

Check Existence of Workspace Variable

Create a variable named |testresults, and then check its existence in the workspace.

testresults = magic(5);
exist testresults var
ans =

     1

A variable named testresults exists in the workspace.

Check if MATLAB Function is Built-In Function

Check whether the plot function is a built-in function or a file.

A = exist('plot')
A =

     5

This indicates that plot is a built-in MATLAB function.

More About

expand all

Tips

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 folder 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 a MATLAB function. 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
Was this topic helpful?