Click or drag to resize
Accord.NET (logo)

MatReader Class

Reader for .mat files (such as the ones created by Matlab and Octave).
Inheritance Hierarchy
SystemObject
  Accord.IOMatReader

Namespace:  Accord.IO
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public class MatReader : IDisposable
Request Example View Source

The MatReader type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyBaseStream
Returns the underlying stream.
Public propertyBigEndian
Gets whether the MAT file uses the Big-Endian standard for bit-order. Currently, only little endian is supported.
Public propertyCode exampleDescription
Gets the human readable description of the MAT file.
Public propertyFieldNames
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.
Public propertyFields
Gets the child nodes contained in this file.
Public propertyItemInt32
Gets a child object contained in this node.
Public propertyItemString
Gets a child object contained in this node.
Public propertyTranspose
Gets whether matrices will be auto-transposed to .NET row and column format if necessary.
Public propertyVersion
Gets the version information about the file. This field should always contain 256.
Top
Methods
  NameDescription
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Releases unmanaged resources and performs other cleanup operations before the MatReader is reclaimed by garbage collection.
(Overrides ObjectFinalize.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRead(String)
Reads an object from a given key.
Public methodReadT(String)
Reads an object from a given key.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(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.)
Public Extension MethodToTOverloaded.
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.)
Top
Remarks

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

Examples

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.

See Also