EXIST Test for Existence

Section: Inspection Functions

Usage

Tests for the existence of a variable, function, directory or file. The general syntax for its use is
  y = exist(item,kind)

where item is a string containing the name of the item to look for, and kind is a string indicating the type of the search. The kind must be one of

You can also leave the kind specification out, in which case the calling syntax is
  y = exist(item)

The return code is one of the following:

Note: previous to version 1.10, exist used a different notion of existence for variables: a variable was said to exist if it was defined and non-empty. This test is now performed by isset.

Example

Some examples of the exist function. Note that generally exist is used in functions to test for keywords. For example,
  function y = testfunc(a, b, c)
  if (~exist('c'))
    % c was not defined, so establish a default
    c = 13;
  end
  y = a + b + c;

An example of exist in action.

--> a = randn(3,5,2)

a = 

(:,:,1) = 
    0.0744   -0.7000   -1.8022   -0.0255   -0.2933 
   -0.0152   -0.5401   -0.2030   -0.5437    0.9261 
   -0.4359   -1.2334    0.5156    0.0821   -0.5065 

(:,:,2) = 
    0.7408    0.6302   -0.2798    0.7279    1.7674 
    0.9063   -1.7321   -0.8851   -0.4091   -0.5516 
    1.4880    0.5030    0.6747   -0.5492    1.1867 

--> b = []

b = 
  []
--> who
  Variable Name       Type   Flags             Size
              a    double                    [3 5 2]
              b    double                    [0 0]
--> exist('a')

ans = 
 1 

--> exist('b')

ans = 
 1 

--> exist('c')

ans = 
 0