#api-bindings #mat #matlab

matio-rs

Rust wrapper to MATLAB MAT file I/O library

18 releases (8 stable)

1.3.1 Mar 10, 2023
1.1.2 Dec 29, 2022
0.5.1 Sep 12, 2022
0.2.0 Jul 11, 2022

#198 in Math

Download history 41/week @ 2023-02-06 82/week @ 2023-02-13 67/week @ 2023-02-20 53/week @ 2023-02-27 82/week @ 2023-03-06 59/week @ 2023-03-13 11/week @ 2023-03-20 77/week @ 2023-03-27 22/week @ 2023-04-03 46/week @ 2023-04-10 14/week @ 2023-04-17 63/week @ 2023-04-24 30/week @ 2023-05-01 82/week @ 2023-05-08 77/week @ 2023-05-15 62/week @ 2023-05-22

304 downloads per month
Used in 16 crates (14 directly)

MIT license

1MB
27K SLoC

C 20K SLoC // 0.1% comments FORTRAN Modern 3K SLoC // 0.1% comments Visual Studio Project 1.5K SLoC Rust 744 SLoC // 0.2% comments Automake 585 SLoC // 0.2% comments Objective-C 364 SLoC Shell 207 SLoC // 0.1% comments Visual Studio Solution 112 SLoC C++ 10 SLoC // 0.6% comments

Rust wrapper to MATLAB MAT file I/O library

This crate provides bindings and wrappers for MATIO: MATLAB MAT file I/O C library


lib.rs:

Rust bindings and wrappers for MATIO

This crate provides bindings and wrappers for MATIO: MATLAB MAT file I/O C library

Examples

Saving to a Mat file

use matio_rs::MatFile;
# let file = tempfile::NamedTempFile::new().unwrap();
# let data_path = file.path();
MatFile::save(data_path)?
.var("a", 1i8)?
.var("b", 2f32)?
.var("c", &vec![3u16; 3])?;
# Ok::<(), matio_rs::MatioError>(())

and then loading the data back into Rust

# use matio_rs::MatFile;
# let file = tempfile::NamedTempFile::new().unwrap();
# let data_path = file.path();
# MatFile::save(data_path)?
#   .var("a", 1i8)?
#    .var("b", 2f32)?
#    .var("c", &vec![3u16; 3])?;
let mat_file = MatFile::load(data_path)?;
let a: i8 = mat_file.var("a")?;
let b: f32 = mat_file.var("b")?;
let c: Vec<u16> = mat_file.var("c")?;
# Ok::<(), matio_rs::MatioError>(())

Saving data to a Matlab structure

use matio_rs::{MatFile, Mat, MayBeFrom};
# let file = tempfile::NamedTempFile::new()?;
# let data_path = file.path();
let mat_a = Mat::maybe_from("fa", 123f64)?;
let b = vec![0i32, 1, 2, 3, 4];
let mat_v = Mat::maybe_from("fb", &b)?;
let data = vec![mat_a, mat_v];
let mat_struct = Mat::maybe_from("s", data)?;
let mat_file = MatFile::save(data_path)?;
mat_file.write(mat_struct);
# Ok::<(), matio_rs::MatioError>(())

and then loading the structure fields back into Rust variables

use matio_rs::{MatFile, Mat, MayBeInto};
# use matio_rs::{MayBeFrom};
# let file = tempfile::NamedTempFile::new()?;
# let data_path = file.path();
# let mat_a = Mat::maybe_from("fa", 123f64)?;
# let b = vec![0i32, 1, 2, 3, 4];
# let mat_v = Mat::maybe_from("fb", &b)?;
# let data = vec![mat_a, mat_v];
# let mat_struct = Mat::maybe_from("s", data)?;
# let mat_file = MatFile::save(data_path)?;
# mat_file.write(mat_struct);
let mat_file = MatFile::load(&data_path)?;
let mat: Mat = mat_file.var("s")?;
let a: f64 = mat
.field("fa")?
.get(0).unwrap()
.maybe_into()?;
let b: Vec<i32> = mat
.field("fb")?
.get(0).unwrap()
.maybe_into()?;
# Ok::<(), matio_rs::MatioError>(())

Rust structure with the [MatIO] derive attribute can be dispatched like any other variables:

use matio_rs::{MatFile, MatIO};
# use tempfile::NamedTempFile;

#[derive(Debug, Default, MatIO)]
struct SMat {
a: f64,
b: Vec<u32>,
s: Nested,
}
#[derive(Debug, Default, MatIO)]
struct Nested {
a: f64,
b: Vec<u32>,
}
let n = Nested {
a: 1f64,
b: vec![2, 3, 4, 5],
};
let a = SMat {
a: 1f64,
b: vec![2, 3, 4, 5],
s: n,
};
# let file = NamedTempFile::new().unwrap();
MatFile::save(&file)?.var("a", &a)?;
let aa: SMat = MatFile::load(file)?.var("a")?;
# Ok::<(), matio_rs::MatioError>(())

Saving a Rust vector into a 3D Matlab array

use matio_rs::{MatFile, MatArray};
# use tempfile::NamedTempFile;
# let file = NamedTempFile::new().unwrap();
let data: Vec<_> = (0..24).collect();
MatFile::save(&file)?
.array("array", &data, vec![3, 4, 2])?;
# Ok::<(), matio_rs::MatioError>(())

nalgebra vectors and matrices can be read from and written to Mat files providing the nalgebra feature

use matio_rs::MatFile;
# use tempfile::NamedTempFile;
# let file = NamedTempFile::new().unwrap();
let na_v = nalgebra::DVector::from_iterator(5, 0..5);
MatFile::save(&file).unwrap().var("na_v", &na_v).unwrap();
let v: nalgebra::DMatrix<i32> = MatFile::load(file).unwrap().var("na_v").unwrap();
use matio_rs::MatFile;
# use tempfile::NamedTempFile;
# let file = NamedTempFile::new().unwrap();
let na_m = nalgebra::DMatrix::from_iterator(3, 2, 0..6);
MatFile::save(&file).unwrap().var("na_m", &na_m).unwrap();
let m: nalgebra::DMatrix<i32> = MatFile::load(file).unwrap().var("na_m").unwrap();

Dependencies

~0.5–3MB
~68K SLoC