exist — Return the documented numeric code for a variable, file, folder, builtin, model, or class.
exist searches the active workspace and configured search path and returns a host double scalar code from 0 through 8. The optional documented searchType values are builtin, class, dir, file, and var.
Syntax
exist name
exist name searchType
typeID = exist(name)
typeID = exist(name, searchType)Inputs
name- Type
StringScalar- Required
- Yes
- Description
- Variable/function/file/class name to query.
type- Type
StringScalar- Required
- Yes
- Description
- Documented query kind: builtin|class|dir|file|var.
Returns
typeID- Type
NumericScalar- Description
- Existence code (0,1,2,3,4,5,6,7,8) following MATLAB semantics.
Errors
RunMat:exist:TooManyInputs- When
- More than two total input arguments are provided.
- Message
exist: too many input arguments
RunMat:exist:InvalidName- When
- Name input is not a character vector or string scalar/array scalar.
- Message
exist: name must be a character vector or string scalar
RunMat:exist:InvalidTypeArgument- When
- Type input is not a character vector or string scalar/array scalar.
- Message
exist: type must be a character vector or string scalar
Queries and return codes
The documented return codes are 0 not found or inaccessible, 1 variable, 2 general file, 3 MEX file, 4 loaded or on-path Simulink model, 5 builtin, 6 P-code, 7 folder, and 8 class.
Without searchType, variables and builtins have highest precedence; folders then precede classes and files; remaining file kinds follow function precedence within search-path order.
exist(name,'file') searches files and folders and can therefore return 2, 3, 4, 6, 7, or 0.
name and documented searchType values are host string scalars or character vectors. Integer and logical inputs are inapplicable and reject without conversion.
RunMat supports additional selector aliases and specialized selectors for its own runtime. They are an explicit RunMat-only extension and reject in MATLAB compatibility mode.
exist(handleObject,'handle') with a nontext first argument is a separate RunMat-only extension.
Arbitrary unauthenticated HTTP and HTTPS URL lookup, JVM class discovery, and loaded Simulink runtime state remain environment-level gaps. Local and configured RunMat search-path lookup is supported.
Calling forms
name is a string scalar or character vector.
searchType is one of builtin, class, dir, file, or var.
The command form writes the result through normal command output behavior; the function form returns typeID.
One result is returned as a host double scalar. Requesting more than one output rejects before workspace or filesystem lookup; requesting zero outputs still performs the query.
Character rows may have trailing singleton dimensions. Genuine character pages, numeric names, and resident numeric inputs are not text and reject before provider access.
Lookup reads the active workspace, class registry, filesystem, and search path. These session-dependent reads form a fusion boundary and are not scheduled as distributed pure computations.
Examples
Check a workspace variable
alpha = 1;
typeID = exist('alpha','var');
assert(typeID == 1);Check a folder through the file filter
typeID = exist('logs','file');
assert(typeID == 7);Check a builtin
typeID = exist('sin','builtin');
assert(typeID == 5);Distinguish files, P-code, and wildcard names
assert(exist('data.txt','file') == 2);
assert(exist('packed','file') == 6);
assert(exist('*.txt','file') == 0);Using exist with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how exist changes the result.
Run a small exist example, explain the result, then change one input and compare the output.
FAQ
What data type is typeID?⌄
It is a host double scalar whose value is an integer code. It is not typed-integer storage.
Do wildcard names work?⌄
No. Names containing wildcard characters do not perform a wildcard search and return 0.
Does RunMat support every MATLAB environment-backed code?⌄
Filesystem MEX, P-code, Simulink-extension, builtin, folder, file, and RunMat class discovery are represented. JVM discovery, loaded Simulink state, and arbitrary web URL lookup need broader environment infrastructure.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · fileattrib · fileparts · filesep · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · memmapfile.subsasgn · memmapfile.subsref · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · getvaropts · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · setvaropts · setvartype · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Data
commit · data.copy · data.create · data.delete · data.exists · data.export · data.import · data.inspect · data.list · data.move · data.open · DataArray.chunk_shape · DataArray.codec · DataArray.dtype · DataArray.fill · DataArray.name · DataArray.rank · DataArray.read · DataArray.resize · DataArray.shape · DataArray.write · Dataset.array · Dataset.arrays · Dataset.attrs · Dataset.begin · Dataset.get_attr · Dataset.has_array · Dataset.id · Dataset.path · Dataset.refresh · Dataset.set_attr · Dataset.set_attrs · Dataset.snapshot · Dataset.version · DataTransaction.abort · DataTransaction.commit · DataTransaction.create_array · DataTransaction.delete_array · DataTransaction.fill · DataTransaction.id · DataTransaction.resize · DataTransaction.set_attr · DataTransaction.set_attrs · DataTransaction.status · DataTransaction.write
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.