exist - Check existence of variable, script, function, folder,
or class - MATLAB (original) (raw)
Check existence of variable, script, function, folder, or class
Syntax
Description
exist [name](#f5-453632-name)
returns the type ofname
as a number. This list describes the type associated with each value:
- 0 —
name
does not exist or cannot be found for other reasons. For example, ifname
exists in a restricted folder to which MATLAB® does not have access,exist
returns 0. - 1 —
name
is a variable in the workspace. - 2 —
name
is a file with extension.m
,.mlx
, or.mlapp
, orname
is the name of a file with a non-registered file extension (.mat
,.fig
,.txt
). - 3 —
name
is a MEX file on your MATLAB search path. - 4 —
name
is a loaded Simulink® model or a Simulink model or library file on your MATLAB search path. - 5 —
name
is a built-in MATLAB function. This does not include classes. - 6 —
name
is a P-code file on your MATLAB search path. - 7 —
name
is a folder. - 8 —
name
is a class. (exist
returns 0 for Java classes if you start MATLAB with the-nojvm
option.)
MATLAB searches starting at the top of the search path, and moving down until a result is found or the last folder on the path is reached. If more than onename
exists in a folder, MATLAB displays the first instance of name
, according to the Function Precedence Order. Folders are an exception to the function precedence rules. They have precedence over all types except for variables and built-in functions.
For example, if name
matches both a file with a .m
extension and a P-code file, then exist
returns 6, identifying it as a P-code file. If name
matches both a variable and a P-code file, exists returns 1, identifying it as a variable. If name
matches both a folder and a MATLAB function, exist
returns 7
, identifying it as a folder.
exist [name](#f5-453632-name) [searchType](#f5-453632-searchType)
returns the type of name
, restricting results to the specified type, searchType
. If name
of type searchType
does not exist, MATLAB returns 0
.
[typeID](#mw%5F2f3d9e73-5a73-4d8c-9d8f-f61c15988ee3) = exist(___)
returns the number corresponding to the type of name totypeID
.
Examples
Create a variable named testresults
, and then confirm its existence in the workspace.
testresults = magic(5); exist testresults
A variable named testresults
exists in the workspace.
Create the folder myfolder
, and then check its existence as a folder.
mkdir myfolder; exist myfolder dir
If you specify the type as file
, MATLAB® searches for both files and folders, therefore returning the same result.
Check whether the plot
function is a built-in function or a file.
This indicates that plot
is a built-in MATLAB function.
Input Arguments
Name of the variable, script, function, folder, or class, specified as a string scalar or character vector.
To specify name
as a relative path, it must fit one of these descriptions:
- A folder on the search path
- In a folder on the search path
- The current folder
- In the current folder
Files and folders at a remote location, you must specify the full path as a uniform resource locator (URL). Internet URLs must include the protocol type "http://"
or "https://"
. For more information, see Work with Remote Data.
Subfolders of folders on the path are not searched.
If name
specifies a file with a non-registered file extension (.mat
, .fig
,.txt
), include the extension. You can also include an extension to prevent a conflict with other similar filenames. For example,exist file.txt
orexist("file.txt")
.
Note
MATLAB does not examine the contents or internal structure of a file and relies solely on the file extension for classification.
Type of results to search for, specified as one of these values:
searchType | Description | Possible Return Values |
---|---|---|
builtin | Checks only for built-in functions. | 5, 0 |
class | Checks only for classes. | 8, 0 |
dir | Checks only for folders. | 7, 0 |
file | Checks only for files or folders. | 2, 3, 4, 6, 7, 0 |
var | Checks only for variables. | 1, 0 |
Output Arguments
Identifier type, specified as a numeric value. This list describes the type associated with each value:
- 0 — name does not exist or cannot be found for other reasons. For example, if
name
exists in a restricted folder to which MATLAB does not have access,exist
returns 0. - 1 —
name
is a variable in the workspace. - 2 —
name
is a file with extension.m
,.mlx
, or.mlapp
, orname
is the name of a file with a non-registered file extension (.mat
,.fig
,.txt
). - 3 —
name
is a MEX file on your MATLAB search path. - 4 —
name
is a loaded Simulink model or a Simulink model or library file on your MATLAB search path. - 5 —
name
is a built-in MATLAB function. This does not include classes. - 6 —
name
is a P-code file on your MATLAB search path. - 7 —
name
is a folder. - 8 —
name
is a class. (exist
returns 0 for Java classes if you start MATLAB with the-nojvm
option.)
Limitations
- MATLAB does not support internet URLs that require authentication.
- MATLAB Online™ supports internet URLs associated with Microsoft® OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.
Alternative Functionality
- To check the existence of a file or folder, you also can use the isfolder or isfile functions.
exist
searches for files and folders on the search path, which can lead to unexpected results. isfolder and isfile search for files or folders only on the specified path or in the current folder, which can lead to clearer and faster results.
Extended Capabilities
Usage notes and limitations:
- The input arguments must be string or character literals.
- You must provide the
searchType
input argument and only the"var"
,"file"
, and"dir"
search types. - When searching for a file or folder, the code generator treats the
exist
function as extrinsic. Therefore, the"file"
and"dir"
search types are not supported for standalone code generation. To check the existence of a file or folder in standalone code generation, use the isfolder and isfile functions. - If you call
exist
by using a function handle, code generation does not support the"var"
search type. - Searching for a local variable in a function is supported only if this variable is defined on all or no execution paths.
- Searching for local variables is not supported inside loops, nested functions, or anonymous functions.
- Searching for varargout and repeating output arguments is not supported.
- In code generation, a global or persistent variable exists everywhere in the body of the function in which the variable is declared. By contrast, in MATLAB execution, a global or persistent variable exists only after it is declared in the function body.
Version History
Introduced before R2006a
You can read data from primary online sources by performing file read operations over an internet URL.
You can generate code for exist
function calls that search for variables, with some limitations. When searching for a file or folder, the code generator treats the exist
function as extrinsic.