1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Serde-based reading and writing of MATLAB v7.3 `.mat` files.
//!
//! MAT v7.3 files are HDF5 files with MATLAB conventions:
//! - A 512-byte userblock starting with the `MATLAB 7.3 MAT-file...` signature
//! - Every dataset and group carries a `MATLAB_class` attribute (`"double"`,
//! `"char"`, `"struct"`, ...)
//! - Strings are stored as `uint16` datasets encoding UTF-16LE
//! - 2-D arrays are laid out column-major (Fortran order); HDF5 shape is
//! `[cols, rows]` so that MATLAB sees the intended `[rows, cols]`
//!
//! # Required top-level shape
//!
//! The outer Rust value must be a **struct with named fields**. Each field
//! becomes a top-level MATLAB variable. This matches `scipy.io.savemat` and
//! MATLAB's workspace model.
//!
//! ```no_run
//! use hdf5_pure::mat;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, Debug, PartialEq)]
//! struct Experiment {
//! name: String,
//! trial: u32,
//! samples: Vec<f64>,
//! }
//!
//! let e = Experiment {
//! name: "run1".into(),
//! trial: 3,
//! samples: vec![1.0, 2.0, 3.0],
//! };
//!
//! let bytes = mat::to_bytes(&e).unwrap();
//! let back: Experiment = mat::from_bytes(&bytes).unwrap();
//! assert_eq!(back, e);
//! ```
//!
//! # Data model
//!
//! | Rust | HDF5 / MATLAB |
//! |---|---|
//! | `f64`, `f32`, `i*`, `u*` | scalar dataset `[1,1]`, `MATLAB_class` = `"double"` etc. |
//! | `bool` | `uint8` scalar, `MATLAB_class = "logical"` |
//! | `String` | `uint16` `[1, N]` UTF-16LE, `MATLAB_class = "char"` |
//! | `Vec<T>` of numeric `T` | `[1, N]` row vector |
//! | [`Matrix`]`<T>` or `Vec<Vec<T>>` | column-major 2-D dataset |
//! | [`Complex32`] / [`Complex64`] | compound `{real, imag}` dataset |
//! | nested struct | group with child datasets, `MATLAB_class = "struct"` |
//! | `Option<T>` | field is omitted when `None` |
//! | unit enum variants | UTF-16 char dataset containing the variant name |
//!
//! See the crate-level README for a fuller description.
pub
pub use MatClass;
pub use ;
pub use MatError;
pub use Matrix;
use DeserializeOwned;
use Serialize;
/// Serialize `value` to a MAT v7.3 byte vector.
///
/// The root value must be a struct with named fields. Each field becomes a
/// top-level MATLAB variable.
Sized>
/// Serialize `value` to the given filesystem path as a MAT v7.3 file.
Sized, P: >
/// Deserialize a MAT v7.3 file from a byte slice.
/// Deserialize a MAT v7.3 file from the filesystem.