RunMat
GitHub

clearvars — Clear workspace variables with optional exclusions in MATLAB and RunMat.

clearvars removes variables from the active workspace, with optional -except exclusions. Named and exclusion forms follow MATLAB semantics for selectively clearing bindings.

Syntax

clearvars()
clearvars(name_or_option, ...)

Inputs

NameTypeRequiredDefaultDescription
name_or_optionStringScalarVariadicVariable names and options such as '-except'.

Returns

NameTypeDescription
ansNumericArrayEmpty 0x0 return value.

Errors

IdentifierWhenMessage
The '-except' option appears more than once.clearvars: duplicate -except option
An unknown option beginning with '-' is provided.clearvars: unsupported option
The '-except' option is provided without exclusion names.clearvars: -except requires at least one variable name

How clearvars works

  • clearvars with no inputs clears all workspace variables created in the current session.
  • clearvars x removes the variable x while leaving other workspace bindings intact.
  • Multiple names can be supplied as separate inputs or command-form tokens, for example clearvars x y z.
  • clearvars -except x y removes all active workspace variables except x and y.
  • clearvars a b c -except b removes only selected variables a and c, preserves selected exclusion b, and leaves variables outside the selected clear set untouched.
  • Function-call forms accept variable names as string scalars, row character vectors, or string arrays.
  • Clearing a name that does not exist is a no-op.
  • clearvars is a sink builtin and does not produce a meaningful output value.

Does RunMat run clearvars on the GPU?

clearvars performs host-side workspace mutation only. It does not launch GPU kernels, does not participate in fusion, and does not depend on acceleration providers.

Examples

Clear the entire interactive workspace

x = 1;
y = magic(3);
clearvars

Expected output:

% x and y are removed from the workspace

Clear selected variables

a = 1;
b = 2;
c = 3;
clearvars a b

Expected output:

% a and b are removed; c remains defined

Keep selected variables while clearing the rest

raw = 1;
answer = 42;
tmp = 3;
clearvars -except answer

Expected output:

% answer remains defined; raw and tmp are removed

Clear selected variables while preserving exclusions

a = 1;
b = 2;
c = 3;
untouched = 4;
clearvars a b c -except b

Expected output:

% a and c are removed; b and untouched remain defined

Using clearvars with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how clearvars changes the result.

Run a small clearvars example, explain the result, then change one input and compare the output.

FAQ

How is clearvars different from clear?

clearvars focuses on workspace variables and supports the -except option for preserving named bindings while clearing the rest.

Can I clear only one variable?

Yes. Use clearvars x or clearvars("x") to remove one variable, and pass multiple names to clear more than one binding in a single call.

Does clearvars -except x clear figures or the console?

No. It only mutates workspace variables. Use close all for figures and clc for the visible console.

Does GPU residency matter?

No. clearvars operates on session bookkeeping, not on array kernels or provider-managed computations.

class · clear · isa · ischar · isstring · which · who · whos

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.