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 = 
  <double>  - size: [3 5 2]
(:,:,1) = 
 
Columns 1 to 3
 -1.218553558434237294  -0.058665539142637134   1.035341956849705314  
 -0.499147758110993933   1.313015386207676327   0.491732208629773127  
  0.353746340101413204   0.561773300674407672   0.038771107253223712  
 
Columns 4 to 5
  0.972527520109172872  -0.727259104935995548  
  0.785593925641515534  -0.977203621921190591  
 -2.796089025580261289  -1.267146737297288217  
(:,:,2) = 
 
Columns 1 to 3
  1.032311919864423055   0.934652433302886942  -0.797640107843293311  
  0.735104330883378565  -0.078653779371251842   0.246197536758750335  
 -1.477029823428393618  -1.650750032221346197  -0.610267072493031359  
 
Columns 4 to 5
  0.784172706704981515   3.208972433336426011  
 -1.588329627273568523   0.579883457004424785  
 -1.514976216510938611   0.322389326974807544  
--> b = []
b = 
  <double>  - size: [0 0]
  []
--> who
  Variable Name      Type   Flags             Size
              a    double                    [3 5 2]
            ans   logical                    [1 1]
              b    double                    [0 0]
              c     int32                    [1 3]
              f    string                    [1 5]
              p    double                    [1 256]
              x      cell                    [2 1]
              y    struct                    [1 1]
--> exist('a')
ans = 
  <int32>  - size: [1 1]
 1  
--> exist('b')
ans = 
  <int32>  - size: [1 1]
 1  
--> exist('c')
ans = 
  <int32>  - size: [1 1]
 1