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. In RunMat mode, typed integer figure numbers are exact structural selectors; compatibility mode accepts ordinary double figure numbers.
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 | Scalar double status: 1 when the requested close operation completes, 0 when it is refused. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:close:InvalidArgument | Close target values are invalid or unsupported. | close: invalid argument |
RunMat:close:InvalidHandle | A structure target is not a valid networking resource handle. | close: invalid handle |
| — | Internal networking, gather, or plotting close processing fails. | close: internal error |
How close works
closewith no arguments closes the current figure when one exists. If no figure exists, it is a successful no-op that returns1without creating 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.- Plotting forms return scalar double
1after a successful or no-op close. A0status is reserved for refused closure, which RunMat cannot yet produce becauseCloseRequestFcncallbacks are not implemented. Networking inputs return1when at least one resource 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. Resident numeric arguments are not networking resources and are rejected as non-host figure targets before provider dispatch.
- In RunMat mode, networking resources may be passed as separate arguments or wrapped in nested cell arrays, and networking selectors may be scalar strings. Separate variadic targets are rejected in compatibility mode; plotting figure targets are not accepted through cell containers in the current implementation.
- In RunMat mode, scalar and array figure numbers can use any of the eight integer classes. Values are read exactly, must be positive, and must fit RunMat's
uint32figure-number range. Compatibility mode rejects typed integer figure numbers because the public compatibility contract does not advertise integer classes.
Does RunMat run close on the GPU?
close performs only CPU-side bookkeeping. Resident numeric values cannot be networking resources and bypass the networking gather path; plotting then rejects them as non-host figure targets without provider dispatch.
Examples
Start a script safely when no figures are open
clc;
clear;
status = closeExpected output:
status = 1Close the current figure
figure(7);
status = closeExpected output:
status = 1Close 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 successful no-op when there is no current figure, returning 1 without creating a default figure or raising an invalid-handle error.
What does the return value represent?⌄
Figure forms return scalar double 1 when the requested operation succeeds or has nothing to close. MATLAB can return 0 when a close callback refuses the request; RunMat does not yet implement that callback path. Networking resources return 1 when at least one client or server was closed and 0 otherwise.
Can I pass multiple clients or servers at once?⌄
Yes in RunMat mode. Pass them as separate arguments (close(client1, client2)) or wrap them in a cell array. Separate variadic targets are a RunMat extension; compatibility mode accepts one target argument.
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?⌄
Resident numeric values are not networking resources and are rejected as non-host figure targets before provider access. Networking structs and plotting state remain host-resident.
Is it safe to call close twice?⌄
It depends on the target. Repeating an explicit figure-number close raises the missing-handle diagnostic, while bare close with no current figure is a successful no-op returning 1. Repeating a networking close returns 0 when no resource remained to close.
Can figure numbers use integer classes?⌄
Yes in RunMat mode. All eight signed and unsigned integer classes are accepted exactly as host structural selectors within the positive uint32 range. Compatibility mode requires an ordinary double figure number.
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.