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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
target | Any | Yes | — | Figure target, tcp resource handle, option token, or target container. |
targets | Any | Variadic | — | One or more close targets. |
Returns
| Name | Type | Description |
|---|---|---|
result | NumericScalar | Closed handle for single-target calls or count/status for multi/all closures. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:close:InvalidArgument | Close target values are invalid or unsupported. | close: invalid argument |
How close works
closewith no arguments closes the current figure when one exists. If no figure exists, it returns0and does not create or error on a default figure.close('all')closes every open figure. Networking resources use explicit selectors such asclose('clients')andclose('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 structtthat was previously returned bytcpclientoraccept. Subsequent socket operations on that client raise MATLAB-style “not connected” diagnostics.close(s)closes the TCP server structsthat was returned bytcpserver. 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 byaccept). Likewise,close('servers')closes every TCP server. Use those explicit networking selectors instead ofclose('all'), which is reserved for figure closure.- Multiple inputs are processed from left to right. The return value is
1when at least one handle was closed and0otherwise. - Invalid arguments raise
RunMat:close:InvalidArgument. Structs that do not contain the hidden RunMat networking identifiers raiseRunMat: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.
closewalks 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 = closeExpected output:
status = 0Close the current figure
figure(7);
status = closeExpected output:
status = 7Close a TCP client after finishing I/O
client = tcpclient("127.0.0.1", 50000);
status = close(client)Expected output:
status = 1Close 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 clientExpected output:
status = 1Close every open TCP client
tcpclient("localhost", 40000);
tcpclient("localhost", 40001);
status = close("clients")Expected output:
status = 1Close networking resources by selector
client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
clientStatus = close("clients");
serverStatus = close("servers")Expected output:
clientStatus = 1
serverStatus = 1Calling close on an already-closed client
client = tcpclient("127.0.0.1", 50000);
close(client);
status = close(client); % nothing left to closeExpected output:
status = 0Close handles stored in a cell array
client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
handles = {client, srv};
status = close(handles)Expected output:
status = 1Using 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.
- View the source for close in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.