close — Close figures or networking resources in MATLAB and RunMat.

close releases plotting figures and TCP clients/servers. Bare close targets the current figure and is a no-op when no figures exist, so common startup code such as clc; clear; close is safe. Object and selector forms return status codes indicating whether handles were closed.

Syntax

result = close()
result = close(target)
result = close(targets...)

Inputs

NameTypeRequiredDefaultDescription
targetAnyYesFigure target, tcp resource handle, option token, or target container.
targetsAnyVariadicOne or more close targets.

Returns

NameTypeDescription
resultNumericScalarClosed handle for single-target calls or count/status for multi/all closures.

Errors

IdentifierWhenMessage
RunMat:close:InvalidArgumentClose target values are invalid or unsupported.close: invalid argument

How close works

  • close with no arguments closes the current figure when one exists. If no figure exists, it returns 0 and does not create or error on a default figure.
  • close('all') closes every open figure. Networking resources use explicit selectors such as close('clients') and close('servers').
  • close(fig) closes an explicit figure handle and raises a figure-handle diagnostic if that explicit handle does not exist.
  • close(t) closes the TCP client struct t that was previously returned by tcpclient or accept. Subsequent socket operations on that client raise MATLAB-style “not connected” diagnostics.
  • close(s) closes the TCP server struct s that was returned by tcpserver. Any clients that were accepted from that server are also disconnected to match MATLAB’s lifecycle rules.
  • close('clients') closes every registered TCP client (including those produced by accept). Likewise, close('servers') closes every TCP server. Use those explicit networking selectors instead of close('all'), which is reserved for figure closure.
  • Multiple inputs are processed from left to right. The return value is 1 when at least one handle was closed and 0 otherwise.
  • Invalid arguments raise RunMat:close:InvalidArgument. Structs that do not contain the hidden RunMat networking identifiers raise RunMat:close:InvalidHandle.
  • Networking happens on the CPU. If the arguments live on the GPU, RunMat gathers them automatically before touching the host-side registries.
  • Arguments wrapped in cell arrays (possibly nested) or scalar string arrays are supported. close walks each element in-order and applies the usual rules to every value it discovers.

Does RunMat run close on the GPU?

close performs only CPU-side bookkeeping. When network structs or option strings reside on the GPU, RunMat gathers them to host memory before inspecting their hidden identifiers. No provider hooks participate, and GPU residency is irrelevant to the close operation.

Examples

Start a script safely when no figures are open

clc;
clear;
status = close

Expected output:

status = 0

Close the current figure

figure(7);
status = close

Expected output:

status = 7

Close a TCP client after finishing I/O

client = tcpclient("127.0.0.1", 50000);
status = close(client)

Expected output:

status = 1

Close a TCP server and any accepted clients

srv = tcpserver("127.0.0.1", 0);
client = accept(srv);             % accept a pending connection
status = close(srv);              % closes the server and the accepted client

Expected output:

status = 1

Close every open TCP client

tcpclient("localhost", 40000);
tcpclient("localhost", 40001);
status = close("clients")

Expected output:

status = 1

Close networking resources by selector

client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
clientStatus = close("clients");
serverStatus = close("servers")

Expected output:

clientStatus = 1
serverStatus = 1

Calling close on an already-closed client

client = tcpclient("127.0.0.1", 50000);
close(client);
status = close(client);   % nothing left to close

Expected output:

status = 0

Close handles stored in a cell array

client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
handles = {client, srv};
status = close(handles)

Expected output:

status = 1

Using close with coding agents

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

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

FAQ

Is bare close safe when no figures are open?

Yes. RunMat treats bare close as a no-op when there is no current figure, returning 0 instead of creating a default figure or raising an invalid-handle error.

What does the return value represent?

For figure targets, the return value is the closed figure handle for a single target or a count for multi-target/all closures. For networking resources, it is 1 when at least one client or server was closed and 0 otherwise.

Can I pass multiple clients or servers at once?

Yes. Pass them as separate arguments (close(client1, client2)) or wrap them in a cell array. The function closes each handle in order and returns 1 when any of the handles required closing.

Does closing a server also close accepted clients?

Yes. RunMat mirrors MATLAB by closing every client that originated from the server before releasing the listener itself.

What happens if I pass a non-network struct?

The builtin raises RunMat:close:InvalidHandle. Only structs produced by the RunMat networking builtins (tcpclient, tcpserver, accept) carry the hidden identifiers that close recognises.

Do GPU arrays require special handling?

No. RunMat gathers GPU-resident values automatically before inspecting them, and networking always runs on the CPU.

Is it safe to call close twice?

Yes. The second call simply returns 0, indicating that nothing needed to be closed.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how close 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.