RunMat
GitHub

exist — Determine whether a variable, file, folder, built-in function, or class exists and return MATLAB-compatible identifiers.

exist(name) returns an integer code that identifies which kind of item named name is currently available. The codes match MATLAB:

| Code | Meaning | | ---- | ----------------------------------------- | | 0 | Item not found | | 1 | Variable in the active workspace | | 2 | File (including scripts and most data files) | | 3 | MEX-file | | 4 | Simulink® model (.slx / .mdl) | | 5 | Built-in MATLAB function | | 6 | P-code (.p / .pp) | | 7 | Folder | | 8 | Class definition |

Pass a second argument to restrict the lookup (for example, 'file', 'dir', 'builtin', 'class', 'mex', 'pcode', 'method', or 'handle'). The builtin mirrors MATLAB semantics for string handling, search order, and numeric return values.

How exist works in RunMat

  • Searches follow MATLAB's precedence: variables first, then built-ins, classes, compiled code (MEX/P-code), standard files, and finally folders.
  • When you omit the type, exist returns the first match respecting this precedence.
  • String handling matches MATLAB: name and the optional type must be character vectors or string scalars. String arrays must contain exactly one element.
  • Paths expand ~ to the user's home folder. Relative paths resolve against the current working directory, and RunMat honours RUNMAT_PATH / MATLABPATH entries when searching for scripts and classes.
  • Package-qualified names such as pkg.func and pkg.Class map to +pkg folders automatically. Class queries also recognise @ClassName folders and .m files that contain classdef.
  • GPU-resident arguments (for example, gpuArray("script")) are gathered automatically, so callers never need to move text back to the CPU manually.
  • Unsupported second-argument types surface a MATLAB-compatible error listing the accepted keywords.

How exist runs on the GPU

The builtin runs entirely on the host CPU. If any argument lives on the GPU, RunMat gathers it to host memory before performing the lookup. Providers do not implement dedicated hooks for exist, and the result is always returned as a host-resident double scalar. Documented behaviour is identical regardless of whether acceleration is enabled.

GPU memory and residency

No. exist gathers any GPU-resident string inputs transparently. The lookup, file system checks, and return value are always host-side operations. Keeping text on the GPU offers no performance benefits, so you can pass regular character vectors or string scalars.

Examples

How to check if a workspace variable exists

alpha = 3.14;
status = exist("alpha", "var")

Expected output:

status =
     1

How to determine if a built-in function is available

code = exist("sin")

Expected output:

code =
     5

How to test whether an M-file is on the MATLAB path

status = exist("utilities/process_data", "file")

Expected output:

status =
     2

How to verify a folder exists before creating logs

mkdir("logs");
status = exist("logs", "dir")

Expected output:

status =
     7

How to detect class definitions in packages

status = exist("pkg.Widget", "class")

Expected output:

status =
     8

How to inspect compiled MEX fallbacks

if exist("fastfft", "mex")
    disp("Using compiled fastfft.");
else
    disp("Falling back to MATLAB implementation.");
end

Expected output:

% Prints which implementation will be executed based on the presence of fastfft.mex*.

FAQ

Does exist support wildcards?

No. Strings containing * or ? return 0, matching MATLAB behaviour.

RunMat reports 4 for .slx / .mdl files. Java class detection is not implemented yet and currently returns 0, mirroring MATLAB when Java support is unavailable.

How are packages handled?

Names like pkg.func translate to +pkg/func.m. Class queries additionally search +pkg/@Class folders.

Is the search path configurable?

Yes. RunMat honours the current working directory plus any folders listed in RUNMAT_PATH or MATLABPATH (path separator aware).

What's the search precedence when multiple items share a name?

Variables have highest priority, followed by built-ins, classes, compiled code (MEX/P-code), standard files, and folders last—exactly like MATLAB.

What happens with unsupported type keywords?

The builtin raises exist: invalid type... describing the accepted tokens, matching MATLAB's diagnostic.

Do handle queries work?

When the first argument is a RunMat handle object, exist(handle, "handle") returns 1. Numeric graphics handles are not yet supported and return 0.

What does exist do in MATLAB?

exist(name, type) checks whether a variable, file, folder, or function named name exists and returns a numeric code indicating what kind of item was found. Zero means nothing was found.

How do I check if a file exists in MATLAB?

Use exist('filename.m', 'file'). It returns 2 if the file is found on the MATLAB path, or 0 if not found. In RunMat, the same syntax works.

What are the return codes for exist in MATLAB?

exist returns 0 (not found), 1 (variable in workspace), 2 (file on path), 3 (MEX file), 4 (Simulink model), 5 (built-in function), 7 (folder), or 8 (Java class). RunMat supports codes 0, 1, 2, 5, and 7.

These functions work well alongside exist. Each page has runnable examples you can try in the browser.

dir, ls, copyfile, pwd, addpath, cd, delete, fullfile, genpath, getenv, mkdir, movefile, path, rmdir, rmpath, savepath, setenv, tempdir, tempname

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how exist works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.