MATLAB Level-5 Mat Files
Level-5 MATLAB Mat files are popular as binary file container for storing one or more matrices. Math.NET Numerics provides basic support for such Mat files with the MathNet.Numerics.Data.Matlab package, which is available on NuGet as separate package and not included in the basic distribution.
Reading matrices from a MATLAB file
The MatlabReader class provides static functions to list all matrices stored in a MAT file or stream,
and to read them individually as Math.NET matrices:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: |
|
Alternatively the reader can list all matrices of a file into named data elements, which can then be read into matrices individually. This is useful e.g. if we need to read some of the matrices to a different type:
1: 2: 3: |
|
Writing matrices to a MATLAB file
The dual to the reader above is the MatlabWriter class that can serialize matrices
to a MATLAB file or stream. Like the reader, the writer can use MatlabMatrix data elements
to compose packed matrices into a file. Each matrix has a name which must not contain spaces.
1: 2: 3: 4: |
|
But there are also direct routines if only a single matrix or matrices of all the same data type are to be stored in a file:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: |
|
Alternatives
The data extension packages also offer other ways to serialize a matrix to a text file. Among others: