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

- `'builtin'` checks for built-in functions
- `'dir'` checks for directories
- `'file'` checks for files
- `'var'` checks for variables
- `'all'` checks all possibilities (same as leaving out `kind`)

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:

- 0 - if `item` does not exist
- 1 - if `item` is a variable in the workspace
- 2 - if `item` is an M file on the search path, a full pathname to a file, or an ordinary file on your search path
- 5 - if `item` is a built-in FreeMat function
- 7 - if `item` is a directory

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
      0.88871349656451581  -0.27486926931161132  -0.12024249625514018
     -0.90519861711237160   0.26884652521190833   1.90471605852950709
     -1.65189724917687153   0.16892429650998678   0.51341456129698237

    Columns 4 to 5
      0.23474524092873861   0.28152354823275738
     -0.05325434204406199  -1.61961083433645769
     -0.57950864136569458   0.78628503285432572
    (:,:,2) =

    Columns 1 to 3
      0.82461267864970456  -0.58233963047628279  -0.69861236877830091
     -0.50220881813883467   2.43683687159149187   1.26785652397925097
     -0.99659329315144363  -0.55300573033678591  -0.33252184524739037

    Columns 4 to 5
      0.35908413820778551  -2.59873004781618944
     -1.47481790392009282  -0.42393856108254580
      2.29840609365120008   0.50244492229832827
    --> 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