MatReader Class |
Namespace: Accord.IO
The MatReader type exposes the following members.
Name | Description | |
---|---|---|
MatReader(Byte, Boolean, Boolean) |
Creates a new MatReader.
| |
MatReader(BinaryReader, Boolean, Boolean) |
Creates a new MatReader.
| |
MatReader(Stream, Boolean, Boolean) |
Creates a new MatReader.
| |
MatReader(String, Boolean, Boolean) |
Creates a new MatReader.
|
Name | Description | |
---|---|---|
BaseStream |
Returns the underlying stream.
| |
BigEndian |
Gets whether the MAT file uses the Big-Endian
standard for bit-order. Currently, only little
endian is supported.
| |
Description |
Gets the human readable description of the MAT file.
| |
FieldNames |
Gets the name of the variables contained in this file. Those
can be used as keys to the Fields property to
retrieve a variable or navigate the variable hierarchy.
| |
Fields |
Gets the child nodes contained in this file.
| |
ItemInt32 |
Gets a child object contained in this node.
| |
ItemString |
Gets a child object contained in this node.
| |
Transpose |
Gets whether matrices will be auto-transposed
to .NET row and column format if necessary.
| |
Version |
Gets the version information about the file.
This field should always contain 256.
|
Name | Description | |
---|---|---|
Dispose |
Performs application-defined tasks associated with
freeing, releasing, or resetting unmanaged resources.
| |
Dispose(Boolean) |
Releases unmanaged and - optionally - managed resources
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize |
Releases unmanaged resources and performs other cleanup operations before the
MatReader is reclaimed by garbage collection.
(Overrides ObjectFinalize.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Read(String) |
Reads an object from a given key.
| |
ReadT(String) |
Reads an object from a given key.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(Type) | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) | |
ToT | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) |
MAT files are binary files containing variables and structures from mathematical processing programs, such as MATLAB or Octave. In MATLAB, .mat files can be created using its save and load functions. For the moment, this reader supports only version 5 MAT files (Matlab 5 MAT-file).
The MATLAB file format is documented at http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
All the examples below involve loading files from the following URL: https://github.com/accord-net/framework/blob/development/Unit%20Tests/Accord.Tests.Math/Resources/mat/
The first example shows how to read a simple .MAT file containing a single matrix of integer numbers. It also shows how to discover the names of the variables stored in the file and how to discover their types:
// Let's say we would like to load different .mat files which can be found at: // https://github.com/accord-net/framework/blob/development/Unit%20Tests/Accord.Tests.Math/Resources/mat/ // Let's assume they all currently reside in a "localPath" // folder. So let's start by trying to load a 32-bit matrix: string pathInt32 = Path.Combine(localPath, "int32.mat"); // Create a .MAT reader for the file: var reader = new MatReader(pathInt32); // Let's check what is the name of the variable we need to load: string[] names = reader.FieldNames; // should be { "a" } // Ok, so we have to load the matrix called "a". // However, what if we didn't know the matrix type in advance? // In this case, we could use the non-generic version of Read: object unknown = reader.Read("a"); // And we could check it's type through C#: Type t = unknown.GetType(); // should be typeof(int[,]) // Now we could either cast it to the correct type or // use the generic version of Read<> to read it again: int[,] matrix = reader.Read<int[,]>("a"); // The a matrix should be equal to { 1, 2, 3, 4 }
The second example shows how to read a simple .MAT file containing a single matrix of 8-bpp integer values (signed bytes):
// Let's say we would like to load different .mat files which can be found at: // https://github.com/accord-net/framework/blob/development/Unit%20Tests/Accord.Tests.Math/Resources/mat/ // Let's assume they all currently reside in a "localPath" // folder. So let's start by trying to load a 8-bit matrix: string pathInt8 = Path.Combine(localPath, "int8.mat"); // Create a .MAT reader for the file: var reader = new MatReader(pathInt8); // The variable in the file is called "arr" sbyte[,] matrix = reader.Read<sbyte[,]>("arr"); // The arr matrix should be equal to { -128, 127 } // (in case he didn't know the name of the variable, // we would have inspected the FieldNames property: string[] names = reader.FieldNames; // should contain "arr"
The third example shows how to read a more complex .MAT file containing a structure. Structures can hold complex types such as collections of matrices, lists, and strings in a nested hierarchy:
// Let's say we would like to load a .mat file // called "simplestruct.mat". It can be found at // https://github.com/accord-net/framework/blob/development/Unit%20Tests/Accord.Tests.Math/Resources/mat/simplestruct.mat // Let's assume it currently resides in a "localPath" folder string fileName = Path.Combine(localPath, "simplestruct.mat"); // Create a .MAT reader for the file: var reader = new MatReader(fileName); // We can extract some basic information about the file: string description = reader.Description; // "MATLAB 5.0 MAT-file, Platform: PCWIN" int version = reader.Version; // 256 bool bigEndian = reader.BigEndian; // false // Enumerate the fields in the file foreach (var field in reader.Fields) Console.WriteLine(field.Key); // "structure" // We have the single following field var structure = reader["structure"]; // Enumerate the fields in the structure foreach (var field in structure.Fields) Console.WriteLine(field.Key); // "a", "string" // Check the type for the field "a" var aType = structure["a"].ValueType; // byte[,] // Retrieve the field "a" from the file var a = structure["a"].GetValue<byte[,]>(); // We can also do directly if we know the type in advance var s = reader["structure"]["string"].GetValue<string>();
The MatReader class can also read the more complex cell array structures. However, there is no example of this functionality right now, except for those unit tests currently in the project repository. If you would like examples for this feature, please open a new issue at the project's issue tracker.