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
| 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
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, andclose('all')closes both clients and servers.- 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
Close 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 all networking resources at once
client = tcpclient("localhost", 40000);
srv = tcpserver("localhost", 0);
status = close("all")Expected output:
status = 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
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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
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.