Matlab® is a commercial technical computing environment
originally focused primarily on linear algebra computations.
Matlab®'s binary file format can store a wide variety of objects,
from simple vectors and matrices to rich objects and structures.
The MatlabFile
and MatlabStream
classes in the Extreme.Data.Matlab
namespace support reading from files in format version 7.0 or earlier.
File is format version 7.3 or later use the HDF5 format, which
is not supported at this time.
The MatlabFile class
contains static methods for reading one or multiple objects
from a file in .rdata format.
The ReadVector
method reads a vector from a Matlab file. It takes one type argument that is required:
the element type of the vector to read.
The method takes one actual argument.
This may be a string containing the path to the file, or a Stream
that has been opened for reading. If a filename is given, it may be the path
to a local file, or the uri of a resource on the Internet.
This method returns the first matrix found in the file.
Optionally, a second string argument may be passed that specifies the name
of the Matlab variable. This method reads objects from the file until an object
with the specified name is found, and returns it as a matrix, if possible.
The optional last argument specifies
whether the element type of the stored vector should match the specified element type
exactly. The default is , which means
that the read operation will succeed as long as the stored element type can be
cast to the requested element type.
The ReadMatrix
method reads a matrix from the file. It has the same arguments and overloads
as the ReadVector.
The element type must be supplied as a generic type argument.
The actual arguments are the path to the file or resource or the stream to read from,
optionally the name of the variable, and optionally whether the element type should match exactly.
var vector1 = MatlabFile.ReadVector<double>(@"c:\vector.mat");
var matrix1 = MatlabFile.ReadMatrix<double>("http://www.example.com/matrix.mat", "mat");
Dim vector1 = MatlabFile.ReadVector(Of Double)("c:\vector.mat")
Dim matrix1 = MatlabFile.ReadMatrix(Of Double)("www.example.com/matrix.mat", "mat")
No code example is currently available or this language may not be supported.
let vector1 = RdataFile.ReadVector<float>(@"c:\vector.rda")
let matrix1 = RdataFile.ReadMatrix<float>("www.example.com/matrix.rda", "mat")
The ReadVectors
method reads multiple vectors from the file. It takes one type argument that is required:
the element type of the vector to read.
The first is a string containing the path to the file, or a Stream
that has been opened for reading. If a filename is given, it may be the path
to a local file, or the uri of a resource on the Internet.
The second argument is a sequence of strings containing the names of the variables
containing the vectors. This method returns a dictionary that maps the names of the variables
to vectors.
The optional last argument specifies
whether the element type of the stored vectors should match the specified element type
exactly. The default is , which means
that the read operation will succeed as long as the stored element type can be
cast to the requested element type.
The ReadAllVectors
returns a dictionary with all the vectors in the file.
The ReadMatrices
method reads matrices from the file. It has the same arguments and overloads
as the ReadVectors.
The element type must be supplied as a generic type argument.
The actual arguments are the path to the file or resource or the stream to read from,
a sequence of variable names containing the matrices,
and optionally whether the element type should match exactly.
The ReadAllMatrices
returns a dictionary with all the matrices in the file.
Using Matlab Data Streams
Matlab data streams are implemented by the MatlabStream
class. This class has no constructors. Instead, use one of the methods of the
MatlabFile class.
Streams can be opened for reading.
Opening files for reading
The Open(String)
method opens a file or stream for reading. The only argument is a string or a stream.
If it is a string, it is the path to the file that should be opened, or
the URI of a network or Internet resource. If it is a stream, then it specifies
the data stream that the objects should be read from.
The methods for reading objects from streams are similar to those of the
MatlabFile class,
but without the argument that specifies the source.
The ReadVector
method reads a vector from a data file. It takes one type argument that is required:
the element type of the vector to read.
Optionally, a string argument may be passed that specifies the name
of the Matlab variable. This method reads objects from the file until an object
with the specified name is found, and returns it as a vector, if possible.
If an object with the name was read previously, it is returned as a vector
without reading more from the stream.
The optional last argument specifies
whether the element type of the stored vector should match the specified element type
exactly. The default is , which means
that the read operation will succeed as long as the stored element type can be
cast to the requested element type.
The ReadMatrix
method reads a matrix from the file. It has the same arguments and overloads
as the ReadVector.
The element type must be supplied as a generic type argument.
The actual arguments are both optional: the name of the variable,
and whether the element type should match exactly.
using (var s2 = MatlabFile.Open(@"c:\sample.mat"))
{
var vector1 = s2.ReadVector<double>();
var matrix1 = s2.ReadMatrix<double>("mat");
}
Using s2 = MatlabFile.Open("c:\sample.mat")
Dim vector1 = s2.ReadVector(Of Double)()
Dim matrix1 = s2.ReadMatrix(Of Double)("a")
End Using
No code example is currently available or this language may not be supported.
use s2 = MatlabFile.Open(@"c:\sample.mat")
let vector1 = s2.ReadVector<float>()
let matrix1 = s2.ReadMatrix<float>("a")
The ReadVectorsT
method multiple vectors from the file. It takes one type argument that is required:
the element type of the vector to read.
The first argument is a sequence of names of the variables in the file
that contain the vectors. The optional second argument specifies
whether the element type of the stored vectors should match the specified element type
exactly. The default is , which means
that the read operation will succeed as long as the stored element type can be
cast to the requested element type.
The ReadAllVectorsT
returns a dictionary with all the vectors in the file.
The ReadMatricesT
method reads matrices from the file. It has the same arguments and overloads
as the ReadVectorsT.
The element type must be supplied as a generic type argument.
The actual arguments are a sequence of variable names containing the matrices,
and optionally whether the element type should match exactly.
The ReadAllMatricesT
returns a dictionary with all the matrices in the file.