主要内容

exist

Check existence of variable, script, function, folder, or class

Description

example

existname返回类型nameas a number. This list describes the type associated with each value:

  • 0 —namedoes not exist or cannot be found for other reasons. For example, ifnameexists in a restricted folder to which MATLAB®does not have access,existreturns 0.

  • 1-nameis a variable in the workspace.

  • 2 —nameis a file with extension上午,.mlx文件, or上午lapp, orname是具有未注册文件扩展名的文件的名称(上午at,.fig,.txt文件).

  • 3 —nameis a MEX-file on your MATLAB search path.

  • 4 —nameis a loaded Simulink®model or a Simulink model or library file on your MATLAB search path.

  • 5 —nameis a built-in MATLAB function. This does not include classes.

  • 6 —nameis a P-code file on your MATLAB search path.

  • 7 —nameis a folder.

  • 8 —nameis a class. (existreturns 0 for Java classes if you start MATLAB with the-nojvm选项。)

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 onenameexists in a folder, MATLAB displays the first instance ofname, according to theFunction 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, ifnamematches both a file with a上午extension and a P-code file, thenexistreturns 6, identifying it as a P-code file. Ifnamematches both a variable and a P-code file, exists returns 1, identifying it as a variable. Ifnamematches both a folder and a MATLAB function,existreturns7, identifying it as a folder.

example

existnamesearchType返回类型name,将结果限制为指定类型,searchType. Ifnameof typesearchTypedoes not exist, MATLAB returns0.

example

A=存在(___)返回类型nametoA.

Examples

collapse all

Create a variable named试验结果,然后确认它在工作区中是否存在。

试验结果= magic(5); exist试验结果
ans = 1

A variable named试验结果exists in the workspace.

创建文件夹myfolder, and then check its existence as a folder.

mkdirmyfolder;存在myfolderdir
ans = 7

如果将类型指定为file,MATLAB®搜索文件和文件夹,因此返回相同的结果。

existmyfolderfile
ans = 7

检查是否plotfunction is a built-in function or a file.

A=存在('情节')
A = 5

This indicates thatplotis a built-in MATLAB function.

Input Arguments

collapse all

Name of variable, script, function, folder, or class, specified as a character vector or string scalar.

name可以包含部分路径,但必须是以下路径之一:

  • 搜索路径上的文件夹

  • In a folder on the search path

  • The current folder

  • 在当前文件夹中

Subfolders of folders on the path are not searched.

Otherwise,name必须包含完整路径。

Ifname指定具有未注册文件扩展名的文件(上午at,.fig,.txt文件), include the extension. You can also include an extension to prevent conflict with other similar file names. For example,exist file.txtexist("file.txt").

Note

MATLAB does not examine the contents or internal structure of a file and relies solely on the file extension for classification.

Data Types:烧焦|string

Type of results to search for, specified as one of these values:

searchType Description Possible Return Values

内置

Checks only for built-in functions.

5, 0

class

只检查类。

8, 0

dir

Checks only for folders.

7,0

file

只检查文件或文件夹。

2, 3, 4, 6, 7, 0

var

Checks only for variables.

1,0

替代功能

  • 要检查文件或文件夹是否存在,还可以使用isfolderisfile功能。exist在搜索路径上搜索文件和文件夹,这可能导致意外结果。isfolderisfilesearch for files or folders only on the specified path or in the current folder, which can lead to clearer and faster results.

R2006a之前引入