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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! 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.
//! - Convenience methods and macros for access and creation of data (see [Untyped interface](#untyped-interface)).
//! - Serde interface (see [Typed interface](#typed-interface)).
//!
//! # Introduction
//!
//! MAT-files store structured numerical data in a binary format. This library offers a [serde_json](https://docs.rs/serde_json)-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:
//!
//! - [x] numeric arrays
//! - [x] structure arrays
//! - [x] cell arrays
//! - [x] sparse arrays
//! - [ ] MCOS/Handle/Java objects (not yet supported)
//!
//! # Untyped Interface
//!
//! The enum `MatVariable` is the Rust type representing a MATLAB variable. It has the
//! following variants. see [`MatVariable`] for details about each type.
//!
//! ```rust
//! # use matrw::*;
//! enum MatVariable {
//! NumericArray(NumericArray),
//! SparseArray(SparseArray),
//! StructureArray(StructureArray),
//! CellArray(CellArray),
//! Structure(Structure),
//! // ...
//! }
//! ```
//!
//! ## Writing MAT-files
//!
//! The macro [`matvar`] can be used to conveniently construct a [`MatVariable`] from various Rust types. Similarly, the macro [`matfile`] can be used to construct a [`MatFile`].
//!
//! ```standalone_crate
//! use matrw::{MatFile, MatVariable, matfile, matvar, save_matfile_v7};
//!
//! // scalar number
//! let a: MatVariable = matvar!(1);
//!
//! // Create a MAT-file with variables "a", "b", "c", ...
//! let mat: MatFile = matfile!(
//! // Insert as variable "a" in MAT-file
//! a: a,
//! // Create also some other variables
//! // ...
//! // scalar character
//! b: matvar!('c'),
//! // empty value
//! c: matvar!([]),
//! // vector
//! d: matvar!([1, 2, 3]),
//! // character array
//! e: matvar!("asd"),
//! // 2x2 matrix
//! f: matvar!([
//! [1, 2],
//! [3, 4],
//! ]),
//! // 2x2x2 array
//! g: matvar!([
//! [
//! [1, 2],
//! [3, 4],
//! ],
//! [
//! [5, 6],
//! [7, 8],
//! ],
//! ]),
//! // scalar struct
//! h: matvar!({
//! f1: 42.,
//! f2: "abc",
//! }),
//! // struct array
//! i: matvar!([
//! {
//! f1: 42.,
//! f2: "test",
//! f3: 1.,
//! },
//! {
//! f1: 43.,
//! f2: "test1",
//! f3: 2.,
//! },
//! ]),
//! // empty struct
//! j: matvar!({}),
//! // cell array
//! k: matvar!([
//! "abc",
//! 42.,
//! 1,
//! ])
//! );
//!
//! // Write MAT-file without using compression
//! let _ = save_matfile_v7("test.mat", mat, false);
//!
//! # let _ = std::fs::remove_file("test.mat");
//! ```
//!
//! ## Reading MAT-files
//!
//! A MAT-file is loaded from a file with the function [`load_matfile`].
//! Variables are accessed by indexing their name.
//!
//! A `MatVariable` must be converted into a Rust type for usage. See the [`MatVariable`]
//! documentation for conversion options for different types.
//!
//! ```standalone_crate
//! # use matrw::{matfile, matvar, save_matfile_v7};
//! #
//! # // Create a MAT-file with variables "a", "b", "c", ...
//! # let mat = matfile!(
//! # // scalar number
//! # a: matvar!(1),
//! # // scalar character
//! # b: matvar!('c'),
//! # // empty value
//! # c: matvar!([]),
//! # // vector
//! # d: matvar!([1, 2, 3]),
//! # // character array
//! # e: matvar!("asd"),
//! # // 2x2 matrix
//! # f: matvar!([
//! # [1, 2],
//! # [3, 4],
//! # ]),
//! # // 2x2x2 array
//! # g: matvar!([
//! # [
//! # [1, 2],
//! # [3, 4],
//! # ],
//! # [
//! # [5, 6],
//! # [7, 8],
//! # ],
//! # ]),
//! # // scalar struct
//! # h: matvar!({
//! # f1: 42.,
//! # f2: "abc",
//! # }),
//! # // struct array
//! # i: matvar!([
//! # {
//! # f1: 42.,
//! # f2: "test",
//! # f3: 1.,
//! # },
//! # {
//! # f1: 43.,
//! # f2: "test1",
//! # f3: 2.,
//! # },
//! # ]),
//! # // empty struct
//! # j: matvar!({}),
//! # // cell array
//! # k: matvar!([
//! # "abc",
//! # 42.,
//! # 1,
//! # ])
//! # );
//! #
//! # // Write MAT-file without using compression
//! # let _ = save_matfile_v7("test.mat", mat, false);
//! #
//! use matrw::{load_matfile, OwnedIndex};
//!
//! // Load MAT-file
//! let mat = load_matfile("test.mat").expect("Could not read file.");
//!
//! // Inspect data
//! // Access variable "a" and convert it to i32
//! assert_eq!(mat["a"].to_i32(), Some(1));
//! // Access variable "b" and convert element "2" to i32
//! assert_eq!(mat["d"].elem(2).to_i32(), Some(3));
//! // Access variable "f" and convert element "(1,1)" to i32
//! assert_eq!(mat["f"].elem([1,1]).to_i32(), Some(4));
//! // Access variable "g" and convert element "[1,0,1]" to i32
//! assert_eq!(mat["g"].elem([1,0,1]).to_i32(), Some(7));
//! // Access variable "h", then field "f1" and convert to f64
//! assert_eq!(mat["h"]["f1"].to_f64(), Some(42.));
//! // Access variable "i", then struct index "1" and convert to f64
//! assert_eq!(mat["i"][1]["f3"].to_f64(), Some(2.));
//! // Access variable "k", then cell index "1" and convert to f64
//! assert_eq!(mat["k"][1].to_f64(), Some(42.));
//!
//! # let _ = std::fs::remove_file("test.mat");
//! ```
//!
//! # Typed Interface
//!
//! The typed interface is used, when the structural content of a MAT-file is *known* at compile time. It is provided using the [`serde`] framework.
//!
//! ## Writing MAT-files
//!
//! Types implementing [`serde::Serialize`] can be serialized into [`MatFile`], using the function [`to_matfile`].
//!
//! ```standalone_crate
//! use matrw::{save_matfile_v7, to_matfile};
//! use serde::{Serialize};
//!
//! #[derive(Serialize)]
//! struct MyMat {
//! a: i32,
//! b: char,
//! d: Vec<i32>,
//! e: String,
//! h: S,
//! }
//!
//! #[derive(Serialize)]
//! struct S {
//! f1: f64,
//! f2: String,
//! }
//!
//! let data = MyMat {
//! a: 1,
//! b: 'c',
//! d: vec![1, 2, 3],
//! e: "asd".to_string(),
//! h: S {
//! f1: 42.,
//! f2: "abc".to_string()
//! }
//! };
//!
//! let mat = to_matfile(data).expect("Cannot serialize data");
//! let _ = save_matfile_v7("test.mat", mat, false);
//!
//! # let _ = std::fs::remove_file("test.mat");
//! ```
//!
//! ## Reading MAT-files
//!
//! [`MatFile`] can be deserialized into a custom type implementing [`serde::Deserialize`], using the function [`from_matfile`].
//!
//! ```standalone_crate
//! use matrw::{save_matfile_v7, to_matfile, load_matfile, from_matfile};
//! use serde::{Deserialize};
//! # use serde::{Serialize};
//!
//! #[derive(Deserialize)]
//! # #[derive(Serialize)]
//! struct S {
//! f1: f64,
//! f2: String,
//! }
//!
//! #[derive(Deserialize)]
//! # #[derive(Serialize)]
//! struct MyMat {
//! a: i32,
//! b: char,
//! d: Vec<i32>,
//! e: String,
//! h: S,
//! }
//!
//! # let data = MyMat {
//! # a: 1,
//! # b: 'c',
//! # d: vec![1, 2, 3],
//! # e: "asd".to_string(),
//! # h: S {
//! # f1: 42.,
//! # f2: "abc".to_string()
//! # }
//! # };
//! #
//! # let mat = to_matfile(data).expect("Cannot serialize data");
//! # let _ = save_matfile_v7("test.mat", mat, false);
//! #
//! // Load MAT-file
//! let matfile = load_matfile("test.mat").expect("Cannot not read file.");
//! let mat: MyMat = from_matfile(&matfile).expect("Cannot deserialize data");
//!
//! // Inspect data
//! assert_eq!(mat.a, 1);
//! assert_eq!(mat.d[2], 3);
//! assert_eq!(mat.e, "asd".to_string());
//! assert_eq!(mat.h.f1, 42.);
//!
//! # let _ = std::fs::remove_file("test.mat");
//! ```
//!
pub use ;
pub use OwnedIndex;
pub use ;
pub use ;
pub use ;