write — Write numeric or text payloads to a TCP client connection.
write(t, data, ...) sends binary or text payloads through a tcpclient connection with selectable datatype conversion, byte order, and timeout handling.
Syntax
count = write(client, data)
count = write(client, data, datatype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
client | Any | Yes | — | tcpclient handle struct. |
data | Any | Yes | — | Payload to send. |
datatype | StringScalar | No | class(data) | Data type label (for example "uint8", "double", "char", "string"). |
Returns
| Name | Type | Description |
|---|---|---|
count | NumericScalar | Number of elements written to the socket. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:write:InvalidTcpClient | Client handle is missing, malformed, invalid, or disconnected. | write: invalid tcpclient handle |
RunMat:write:InvalidInput | Argument list shape is unsupported for write. | write: invalid argument list |
RunMat:write:InvalidData | Payload cannot be converted to the requested datatype. | write: invalid data payload |
RunMat:write:InvalidDataType | Datatype argument is not a supported scalar text label. | write: invalid datatype argument |
RunMat:write:NotConnected | Client has no active socket connection. | write: tcpclient is disconnected |
RunMat:write:Timeout | Socket write exceeds configured timeout. | write: timed out while sending data |
RunMat:write:ConnectionClosed | Peer closes socket before payload is fully written. | write: connection closed before all data was sent |
RunMat:write:InternalError | Internal socket/control-flow conversion fails. | write: internal socket error |
How write works
write(t, data)sends values using the datatype ofdata. Double, single, and all eight native integer classes therefore retain their native wire width and signedness.write(t, data, datatype)converts the payload to the supplied datatype before encoding it. Supported values are"uint8","int8","uint16","int16","uint32","int32","uint64","int64","single","double","char", and"string". Integer conversions saturate to the destination range;"char"uses single-byte character codes and"string"encodes UTF-8 text.- The compatibility target documents real row and column vectors. Matrix and multidimensional numeric data, sparse data, and logical data are separate RunMat language extensions available in
runmatmode. - The client’s
ByteOrderproperty controls how multi-byte numeric values are serialised."little-endian"is the default, while"big-endian"matches the traditional network byte order. - When the socket cannot send the entire payload before the timeout expires,
writeraisesRunMat:write:Timeout. If the peer closes the connection before or during the transfer the builtin raisesRunMat:write:ConnectionClosedand marks the client as disconnected. - Automatically resident data is gathered through its owning provider before bytes are written. Explicit
gpuArrayinput is a RunMat language extension and is rejected in MATLAB-compatible modes before provider or socket access. - The current RunMat implementation returns the number of elements written when an output is requested. The compatibility target documents
writewithout an output, so code that assigns this value is RunMat-specific.
Does RunMat run write on the GPU?
Networking occurs on the host CPU. Automatically resident data is gathered through its owning provider before byte conversion. Explicit gpuArray data follows the separately gated RunMat extension policy.
Examples
Sending an array of bytes to an echo service
client = tcpclient("127.0.0.1", 50000);
count = write(client, uint8(1:4))Expected output:
count =
4Writing doubles with explicit byte order
client = tcpclient("localhost", 50001, "ByteOrder", "big-endian");
values = [1.5 2.5 3.5];
write(client, values, "double")Transmitting ASCII text
client = tcpclient("127.0.0.1", 50002);
write(client, "RunMat TCP", "char")Sending UTF-8 encoded strings
client = tcpclient("127.0.0.1", 50003);
write(client, "Διακριτό", "string")Handling connection closures
client = tcpclient("example.com", 12345, "Timeout", 0.25);
try
write(client, uint8([1 2 3 4]));
catch err
disp(err.identifier)
endExpected output:
RunMat:write:ConnectionClosedUsing write with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how write changes the result.
Run a small write example, explain the result, then change one input and compare the output.
FAQ
How many output values does write return?⌄
The current RunMat implementation returns the number of elements written when requested. The compatibility target documents no output, so portable code should call write without assigning a result.
Does write support complex numbers?⌄
No. The input must be real. Pass separate real and imaginary parts or convert to a byte representation manually.
How are values rounded when converting to integer datatypes?⌄
Floating-point inputs are rounded to the nearest integer and then saturated to the target range, matching MATLAB casts (for example uint8(255.7) becomes 256 → 255, int8(-128.2) becomes -128).
What happens to GPU-resident tensors?⌄
Automatically resident tensors are gathered transparently. An explicit gpuArray argument requires runmat mode because the compatibility target does not document interactive device input for this operation.
Can I stream large payloads?⌄
Yes. write loops until the entire payload has been sent or an error occurs. Large payloads honour the client’s timeout and byte-order settings.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Related guides
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how write is executed, line by line, in Rust.
- View the source for write 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.