RunMat
GitHub

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

close releases resources such as plotting figures and TCP clients/servers. Object and selector forms return status codes indicating whether handles were closed, following MATLAB and RunMat close semantics.

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(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, and close('all') closes both clients and servers.
  • 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

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 all networking resources at once

client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
status = close("all")

Expected output:

status = 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

What does the return value represent?

The return value is 1 when at least one client or server was closed and 0 otherwise. Use it to detect whether close released any networking resources.

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.