xlswrite — Write numeric, logical, string, character, or cell data to Excel-compatible spreadsheet files.
xlswrite provides MATLAB's legacy spreadsheet export forms for older scripts. It writes an OOXML workbook payload for .xlsx and legacy .xls filenames, accepts worksheet and range selectors, preserves supported cells in workbooks previously created by RunMat xlswrite, and returns status/message outputs when requested.
Syntax
status = xlswrite(filename, A)
status = xlswrite(filename, A, sheetOrRange)
status = xlswrite(filename, A, sheet, range)
status = xlswrite(filename, A, sheet, range, '-basic')
[status,message] = xlswrite(filename, A)
[status,message] = xlswrite(filename, A, sheetOrRange)
[status,message] = xlswrite(filename, A, sheet, range)
[status,message] = xlswrite(filename, A, sheet, range, '-basic')Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
filename | StringScalar | Yes | — | Spreadsheet file path. |
A | Any | Yes | — | Numeric, logical, string, character, or cell data to write. |
sheetOrRange | Any | Yes | — | Worksheet name/index or Excel A1 range start. |
sheet | Any | Yes | — | Worksheet name or one-based worksheet index. |
range | StringScalar | Yes | — | Excel A1 range start or range. |
mode | StringScalar | No | "basic" | Legacy '-basic' compatibility flag. |
Returns
| Name | Type | Description |
|---|---|---|
status | LogicalArray | True when the spreadsheet was written successfully. |
message | Any | Diagnostic struct with message and identifier fields. |
Returned values from xlswrite depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:xlswrite:InvalidArgument | Argument list does not match supported xlswrite call forms. | xlswrite: invalid argument |
RunMat:xlswrite:Filename | Filename is invalid or cannot be normalized. | xlswrite: invalid filename |
RunMat:xlswrite:Range | Range specification is malformed or exceeds worksheet limits. | xlswrite: invalid range |
RunMat:xlswrite:Sheet | Worksheet selector is invalid. | xlswrite: invalid sheet |
RunMat:xlswrite:Data | Input data cannot be represented as supported spreadsheet cells. | xlswrite: invalid data |
RunMat:xlswrite:Io | Spreadsheet cannot be written. | xlswrite: unable to write spreadsheet |
RunMat:xlswrite:OutputCount | Caller requests more outputs than xlswrite supports. | xlswrite: unsupported output count |
How xlswrite works
xlswrite(filename, A)writesAto the first worksheet starting atA1. If the filename has no extension,.xlsxis appended.xlswrite(filename, A, sheet)selects a worksheet by name or one-based numeric index.xlswrite(filename, A, range)writes to an A1-style range start such as"B2"on the default worksheet.xlswrite(filename, A, sheet, range)combines worksheet and range selection.- When the destination workbook already exists and was created by RunMat
xlswrite,xlswritepreserves supported existing worksheets and cells outside the overwritten range. - Existing unmarked Excel workbooks are rejected instead of being flattened into RunMat's minimal workbook representation; this avoids silently discarding formulas, macros, styles, charts, images, or workbook metadata.
- Numeric sheet selectors are one-based worksheet ordinals. Missing ordinal sheets are created as
SheetNplaceholders so laterxlsread(filename, N, ...)calls address the same worksheet. - Numeric, sparse, logical, string, character, and heterogeneous cell inputs are supported. Cell contents must be scalar spreadsheet values.
- A1 ranges may use absolute markers such as
$B$2:$C$3; malformed range endpoints are rejected. - The legacy
'-basic'mode flag is accepted as a no-op because RunMat writes spreadsheets directly rather than using Excel COM automation. status = xlswrite(...)returns true on success.[status,message] = xlswrite(...)captures write failures as false plus a diagnostic struct withmessageandidentifierfields.
Does RunMat run xlswrite on the GPU?
xlswrite performs file I/O and spreadsheet serialization on the CPU. It is not fusible and does not call acceleration provider hooks.
GPU memory and residency
xlswrite consumes host-side file data. gpuArray inputs are gathered before writing; outputs are CPU-resident status/message values.
Examples
Write a numeric matrix
status = xlswrite('measurements.xlsx', [1 2 3; 4 5 6]);Write to a named worksheet and range
status = xlswrite('measurements.xlsx', {'Voltage', 1.5, true}, 'Data', 'B2');Capture a failure message
[status, message] = xlswrite('out.xlsx', 1+2i);
if ~status
disp(message.message)
endUsing xlswrite with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how xlswrite changes the result.
Run a small xlswrite example, explain the result, then change one input and compare the output.
FAQ
Does xlswrite use Microsoft Excel?⌄
No. RunMat writes workbook files directly on the host. The '-basic' flag is accepted for compatibility, but COM automation is not used.
What happens with .xls filenames?⌄
RunMat accepts legacy .xls filenames and writes the same OOXML workbook payload used for .xlsx. This keeps old scripts running in RunMat and remains readable by RunMat's content-based spreadsheet reader.
Can xlswrite update an existing Excel workbook created elsewhere?⌄
No. RunMat rejects unmarked existing workbooks rather than rewriting them as a minimal value-only workbook and losing formulas, macros, formatting, charts, images, comments, or metadata.
What should I use for new code?⌄
xlswrite exists for legacy MATLAB compatibility. For new code, prefer writematrix, writecell, or table-oriented export APIs where available.
Related Io functions
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread
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
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how xlswrite is executed, line by line, in Rust.
- View the source for xlswrite 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.