RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • fclose
    • feof
    • fgetl
    • fgets
    • fileread
    • filewrite
    • fopen
    • fprintf
    • fread
    • frewind
    • fwrite
    • readlines
    • writelines

filewrite — Write text or raw bytes with RunMat's native file-writing extension.

filewrite(filename, data) is a RunMat-native extension that writes text or raw byte content to a file and returns the number of bytes written. MATLAB does not provide a filewrite builtin; MATLAB-compatible mode therefore rejects this API before gathering data or modifying the filesystem. Enable RunMat extensions to use it.

Syntax

count = filewrite(filename, data)
count = filewrite(filename, data, encoding)
count = filewrite(filename, data, "Encoding", encoding)
count = filewrite(filename, data, ..., "WriteMode", mode)

Inputs

NameTypeRequiredDefaultDescription
filenameAnyYes—Path to target file.
dataAnyYes—Text or uint8-compatible data to write.
encodingStringScalarNo"auto"Positional encoding label.
namePropertyNameVariadic—Option name: 'Encoding' or 'WriteMode'.
valuePropertyValueVariadic—Option value associated with option name.

Returns

NameTypeDescription
countNumericScalarNumber of bytes written.

Errors

IdentifierWhenMessage
RunMat:filewrite:InvalidInputFilename/data argument constraints are violated.filewrite: invalid input arguments
RunMat:filewrite:InvalidOptionName/value options are malformed or unsupported.filewrite: invalid option configuration
RunMat:filewrite:InvalidDataInput data cannot be converted to writable bytes.filewrite: unsupported data payload
RunMat:filewrite:EncodeFailedEncoding selected output bytes/chars fails.filewrite: failed to encode payload
RunMat:filewrite:IoFailureFilesystem open/write/flush operation fails.filewrite: file I/O failed
—Internal runtime control-flow or conversion fails.filewrite: internal error

How filewrite works

  • Accepts file names supplied as character vectors, string scalars, or scalar string arrays.
  • Accepts data supplied as character vectors, string scalars, string arrays, or numeric arrays of bytes. All eight integer classes (int8, int16, int32, int64, uint8, uint16, uint32, and uint64) are read exactly and must lie in the range 0–255; double inputs use the existing integral-byte validation in that range, and logical arrays map to bytes 0 and 1.
  • By default the file is truncated (overwrite mode). Pass 'WriteMode','append' to add to an existing file.
  • Encoding is optional. Provide either a positional encoding argument or the 'Encoding', value keyword pair. Supported encodings mirror fileread: auto (default), utf-8, ascii, latin1, and raw.
  • When writing text data with filewrite and Encoding set to ascii or latin1, an error is raised if any characters fall outside the permitted code page.
  • When writing raw numeric bytes, encoding is ignored except to validate ASCII requests.
  • Returns the number of bytes written as a double scalar.

Does RunMat run filewrite on the GPU?

filewrite performs synchronous host file I/O. Provider-resident numeric data is gathered before writing. No GPU kernels are launched and providers do not need to implement specialised hooks for this RunMat-native builtin.

Examples

Write Text To A New File

bytes = filewrite("notes.txt", "Hello, RunMat!")

Expected output:

bytes = 14

Append Text Without Overwriting

filewrite("log.txt", "First line\n");
bytes = filewrite("log.txt", "Second line\n", 'WriteMode', 'append')

Expected output:

bytes = 13

Specify A Particular Encoding

bytes = filewrite("latin1.txt", ['E' 's' 'p' 'a' char(241) 'a'], 'Encoding', 'latin1')

Expected output:

bytes = 6

Write Raw Bytes From A Numeric Array

payload = [1 2 3 255];
bytes = filewrite("data.bin", payload, 'Encoding', 'raw')

Expected output:

bytes = 4

Export A String Array As Newline-Separated Text

lines = ["alpha", "beta", "gamma"];
bytes = filewrite("items.txt", lines)

Expected output:

bytes = 16

Handle Invalid ASCII Characters

try
    filewrite("ascii.txt", ['c' 'a' 'f' char(233)], 'Encoding', 'ascii');
catch err
    disp(err.message);
end

Expected output:

filewrite: character 'é' (U+00E9) cannot be encoded as ASCII

Using filewrite with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how filewrite changes the result.

Run a small filewrite example, explain the result, then change one input and compare the output.

FAQ

What does filewrite return?⌄

It returns the number of bytes written as a double scalar. Omit the output argument when you do not need this information.

How are string arrays written?⌄

Each element of the string array is written sequentially in column-major order, separated by newline characters. This is RunMat-native behavior.

Does filewrite add a newline automatically?⌄

No. Provide explicit newline characters (\n) when you want line breaks. Appending mode ('WriteMode','append') does not insert separators automatically.

How can I write binary data?⌄

Provide a numeric array with values in the range 0–255 (for example uint8). Use 'Encoding','raw' (or rely on the default) to bypass text encoding.

What happens if the file cannot be opened?⌄

filewrite throws a descriptive error containing the system message (for example, permissions or directory-not-found issues).

Does the builtin create directories?⌄

No. The parent directory must already exist. Use mkdir before calling filewrite if you need to create folders.

Related Io functions

Filetext

fclose · feof · fgetl · fgets · fileread · fopen · fprintf · fread · frewind · fwrite · readlines · writelines

Net

accept · read · readline · tcpclient · tcpserver · write

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

Audio

audioinfo · audioread

Io

clc · diary · disp · display · format · input

Archive

gunzip · gzip · unzip

Hdf5

h5disp · h5info · h5read · h5write · h5writeatt · hdf5info · hdf5read · hdf5write

Import

importdata · textscan

Json

jsondecode · jsonencode

Mat

load · matfile · save

Http

sendmail · urldecode · urlencode · weboptions · webread · websave · webwrite

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how filewrite is executed, line by line, in Rust.

  • View the source for filewrite 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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How filewrite works
  • Does RunMat run filewrite on the GPU?
  • Examples
  • Write Text To A New File
  • Append Text Without Overwriting
  • Specify A Particular Encoding
  • Write Raw Bytes From A Numeric Array
  • Export A String Array As Newline-Separated Text
  • Handle Invalid ASCII Characters
  • Using filewrite with coding agents
  • FAQ
  • Related Io functions
  • Filetext
  • Net
  • Repl Fs
  • Tabular
  • Audio
  • Io
  • Archive
  • Hdf5
  • Import
  • Json
  • Mat
  • Http
  • Open-source implementation
  • About RunMat