matrw

matrw is a pure Rust library for serializing and deserializing MATLAB MAT-files.
Highlights
- Read and write of basic MATLAB array types (numeric, structure, cell, sparse).
- Support of MAT-file compression.
- Convence methods and macros for access and creation of data (see Untyped interface).
- Serde interface (see Typed interface).
Introduction
MAT-files store structured numerical data in a binary format. This library offers a -like interface for ergonomically reading and writing MAT-file data.
Currently, matrw supports serialization and deserialization of version 7 MAT-files. The parser currently handles the following data types:
- numeric arrays
- structure arrays
- cell arrays
- sparse arrays
- MCOS/Handle/Java objects (not yet supported)
[!WARNING] The public API isn’t fully stable yet — breaking changes may occur in future versions.
Quickstart
Two interfaces are available:
- Untyped — if data layout is unknown at compile time.
- Typed — if data layout is known at compile time.
Untyped interface
The untyped interface represents all possible MAT-file data types using the MatVariable enum. The matfile and matvar macros provide convenient ways to construct MAT-file data from native Rust types.
use ;
// Create a MAT-file with variables "a", "b", "c", ...
let mat = matfile!;
// Write MAT-file without using compression
let _ = save_matfile_v7;
// ============================================================================
// Load MAT-file
let mat = load_matfile.expect;
// Inspect data
// Access variable "a" and convert it to i32
assert_eq!;
// Access variable "b" and convert element "2" to i32
assert_eq!;
// Access variable "f" and convert element "(1,1)" to i32
assert_eq!;
// Access variable "g" and convert element "[1,0,1]" to i32
assert_eq!;
// Access variable "h", then field "f1" and convert to f64
assert_eq!;
// Access variable "i", then struct index "1" and convert to f64
assert_eq!;
// Access variable "k", then cell index "1" and convert to f64
assert_eq!;
Typed interface
If the data structure is known at compile time, serialization and deserialization can also be done using the serde interface.
use ;
use ;
let data = MyMat ;
let mat = to_matfile.expect;
let _ = save_matfile_v7;
// ============================================================================
// Load MAT-file
let matfile = load_matfile.expect;
let mat: MyMat = from_matfile.expect;
// Inspect data
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;