This project has moved. For the latest updates, please go here.

MatlabMatrixReader Example?

Jan 16, 2012 at 5:30 PM

Has anyone used MatlabMatrixReader? I am having problems with the syntax.

The following (incorrect) code:

MatlabMatrixReader reader = new MatlabMatrixReader(filename);
Dictionary<string, Matrix<double>> dictionary = (Dictionary<string, Matrix<double>>)reader.ReadMatrices();

does not compile for me. Under MonoDevelop I get the error:

Error CS0308: The non-generic type 'MathNet.Numerics.LinearAlgebra.Double.Matrix' cannot be used with type arguments (CS0308)

And the code:

MatlabMatrixReader reader = new MatlabMatrixReader(filename);
Dictionary<string, Matrix> dictionary = (Dictionary<string, Matrix>)reader.ReadMatrices();
gives me the runtime error:

EXCEPTION: System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,MathNet.Numerics.LinearAlgebra.Generic.Matrix`1[System.Double]]' to type 'System.Collections.Generic.Dictionary`2[System.String,MathNet.Numerics.LinearAlgebra.Double.Matrix]'.

Any thoughts would be appreciated. (Well, not ANY thoughts...)

Jan 16, 2012 at 5:59 PM

The solution is to use the fully qualified matrix type:

Dictionary<string, MathNet.Numerics.LinearAlgebra.Generic.Matrix<double>> dictionary = 
	(Dictionary<string, MathNet.Numerics.LinearAlgebra.Generic.Matrix<double>>)reader.ReadMatrices();

Nice readable code there, but it works.