next up previous contents
Next: ISSPARSE Test for Sparse Up: Inspection Functions Previous: ISSET Test If Variable   Contents

Subsections

EXIST Test for Existence

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 2
   -0.0361639933961680       -0.238187257168569      
   -0.140415140955028         0.599755385896831      
    0.693389551907565         0.708649351074680      
 
Columns 3 to 4
   -0.939406097470966        -0.164794584325194      
   -0.00648807006806828       0.0101167556598398     
   -0.0530953547548948        0.160105749424486      
 
Columns 5 to 5
   -1.465385481298682      
   -0.0395884566172688     
    1.182465366442761      
(:,:,2) = 
 
Columns 1 to 2
   -0.744595958059576        -1.029772319457510      
    0.647296600570314         0.691289627813676      
   -0.403233868578614        -0.513911033581514      
 
Columns 3 to 4
   -0.625280812142897         0.747620555277489      
   -0.121927486686842        -0.844868680099879      
    0.502771253977369         0.438758894856346      
 
Columns 5 to 5
   -0.567515585636788      
    0.840068850555420      
   -1.266240568477841      
--> b = []
b = 
  <double>  - size: []
  []
--> who
  Variable Name      Type   Flags             Size
              a    double                    [3 5 2]
              b    double                    []
            ans    double                    []
--> exist('a')
ans = 
  <int32>  - size: [1 1]
             1  
--> exist('b')
ans = 
  <int32>  - size: [1 1]
             1  
--> exist('c')
ans = 
  <int32>  - size: [1 1]
             0


next up previous contents
Next: ISSPARSE Test for Sparse Up: Inspection Functions Previous: ISSET Test If Variable   Contents
Samit K. Basu 2005-03-16