## Introduction to Matlab exist

In Matlab, the ‘exist’ stands for existence. ‘exist’ function checks the existence of variables, functions, classes, folders, etc. this function helps us to know about the available checklist in the Matlab workspace. If the given file or folder is already present in Matlab, it will create an error while creating a new file with the same name and use the same folder instead of the new folder so that space will be optimized. This function gives integer values as output; This output return value varies from 0 to 8.

**Syntax**

- exist input

exist variable name

- exist (‘max’)

exist (‘inbuilt function name’)

- exist gui file

exist Matlab file name file

- exist until dir

exist folder name dir

### How exist function work in Matlab?

‘Exist’ function returns values in the form of integers. If a given quantity is present in Matlab, then it gives an output from 1 to 8 depending upon the type of quantity. And if the given quantity is not present in Matlab, then it gives the output as ‘0’. We can check the existence of variables, files with extension ‘.m’, ‘.mlx’, ‘.mlapp’, ‘.mat’, ‘.fig’,’.txt’ ), folders as dir, inbuilt functions, classes, etc. if the given variable is present in Matlab workspace then it returns number ‘1’, for files it returns ‘2’ or ‘3’. If it is a simulation model, then it returns 4.if we are checking the existence of the inbuilt function, then it will show presence by ‘5’, and for folders, it will return ‘7’.

### Examples

Here are the following examples mention below

#### Example #1 ( Variables )

In this example, first, we need to create one variable, then we can check the existence of the variable; here, one variable is created, which is ‘input ‘. After variable creation, we have used the existing function on the variable; therefore, the output is 1 that means the ‘input’ variable is present in Matlab workspace, which is illustrated in example 1(a). in example 1(b), without creating any variable, we directly applied the function on the ‘output’ variable, and it returns the result as 0, which means the ‘output ‘ variable is not present in the Matlab workspace.

**Matlab code for Example 1(a) –**

`clc ;`

clear all ;

input = 10

exist input

**Output:**

**Matlab code for Example 1( b ):**

`clc ;`

clear all ;

input = 10

exist output

**Output:**

#### Example #2

In this example, we will check the existence of inbuilt functions from Matlab. There are various functions available in Matlab like max, mean, min, avg, import, disp, use, and so on. Here we have used the max’ function to check existence obviously as we know it is present in Matlab so that it will return output value as ‘5’. And we will use one random name, ‘abc’, it will return output as ‘0’, which means there no such function available in Matlab, which is illustrated in example 2.

4.5 (6,961 ratings)

View Course

**Matlab code for Example 2:**

`clc ;`

clear all ;

disp ('output of inbuilt function which is already present in matlab ')

exist('max')

disp ('output of inbuilt function which is not present in matlab ')

exist('abc')

**Output:**

#### Example #3

This example will check the existence of files in Matlab. To check the present first, we need to create one file, here we have created a Matlab file with the name ‘gui.m’. There is a limitation on extensions to check the existence of files; only a few extensions are allowed like ‘.m’, ‘.mlx’, ‘.mlapp’, ‘.mat’, ‘.fig’,’.txt’. As we have already created a Matlab file with extension .m, then we will get the output as ‘2’. Numbers ‘2’ represent the existence of files. On another side, we have checked the existence of file gui10; it returns the value 0, which means there is no such file with the name gui 10 in the workspace.

**Matlab code for Example 3:**

`clc ;`

clear all ;

% already created one matlab file 'gui.m'

disp ('output of file which is already present in matlab')

exist gui file

disp ('output of file which is not present in matlab workspace')

exist gui10 file

**Output:**

#### Example #4

In this example, we will check the existence of a folder. To check existence in the directory, first, we need to create one folder, here we have created one folder in the directory ‘util’, and after checking existence, it returns the value ‘7’. That means the given folder is present in the directory, and the number ‘7’ represents the folder’s existence in the Matlab workspace. Then we checked the existence of another folder, ‘util10’, which is not present in the directory so that it will return the result as ‘0’.

**Matlab code for Example 4:**

`clc ;`

clear all ;

% already created one matlab folder ' util'

disp ('output of folder which is already present in matlab workspace')

exist util dir

disp ('output of folder which is not present in matlab workspace')

exist util10 dir

**Output:**

### Conclusion

In this article, we have seen various uses of the ‘exist’ function. It checks the existence of almost every factor which is required in stands implementation of any model. There is some alternative to existing a function like ‘is file’ and ‘is the folder’, but instead of using different syntax for each term (variable, files, folders, function, class), it’s good practice to use the same function for all the terms.

### Recommended Articles

This is a guide to Matlab exist. Here we discuss How to exist function work in Matlab and Examples along with the codes and outputs. You may also look at the following articles to learn more –