From 1c9f4f2ba93c6985e84eb0213b97edeec58e9424 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 22 Jul 2014 13:05:39 +0200 Subject: [PATCH] Data: Matlab: MatlabReader.ListMatrices --- src/Data/Matlab/MatlabMatrix.cs | 15 ++--------- src/Data/Matlab/MatlabReader.cs | 48 ++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Data/Matlab/MatlabMatrix.cs b/src/Data/Matlab/MatlabMatrix.cs index ad63cd49..61f89036 100644 --- a/src/Data/Matlab/MatlabMatrix.cs +++ b/src/Data/Matlab/MatlabMatrix.cs @@ -28,32 +28,21 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; -using MathNet.Numerics.LinearAlgebra; - namespace MathNet.Numerics.Data.Matlab { public class MatlabMatrix { - byte[] _data; - + internal byte[] Data { get; private set; } public string Name { get; private set; } public int Size { get; private set; } public int Dimensions { get; private set; } internal MatlabMatrix(string name, int size, int dimensions, byte[] data) { - _data = data; - + Data = data; Name = name; Size = size; Dimensions = dimensions; } - - internal Matrix Read() - where TDataType : struct, IEquatable, IFormattable - { - return Parser.ReadMatrixBlock(_data); - } } } diff --git a/src/Data/Matlab/MatlabReader.cs b/src/Data/Matlab/MatlabReader.cs index f236c3b3..76329dc0 100644 --- a/src/Data/Matlab/MatlabReader.cs +++ b/src/Data/Matlab/MatlabReader.cs @@ -41,6 +41,28 @@ namespace MathNet.Numerics.Data.Matlab /// public static class MatlabReader { + /// + /// List all compatible matrices from a MATLAB file stream. + /// + public static List ListMatrices(Stream stream) + { + return Parser.ParseAll(stream); + } + + /// + /// List all compatible matrices from a MATLAB file. + /// + public static List ListMatrices(string filePath) + { + using (var stream = File.OpenRead(filePath)) + { + return Parser.ParseAll(stream); + } + } + + /// + /// Read the first or a specific matrix from a MATLAB file stream. + /// /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. public static Matrix ReadMatrix(Stream stream, string matrixName = null) where TDataType : struct, IEquatable, IFormattable @@ -49,7 +71,7 @@ namespace MathNet.Numerics.Data.Matlab if (string.IsNullOrEmpty(matrixName)) { - return matrices.First().Read(); + return Parser.ReadMatrixBlock(matrices.First().Data); } var matrix = matrices.Find(m => m.Name == matrixName); @@ -58,9 +80,12 @@ namespace MathNet.Numerics.Data.Matlab throw new KeyNotFoundException("Matrix with the provided name was not found."); } - return matrix.Read(); + return Parser.ReadMatrixBlock(matrix.Data); } + /// + /// Read the first or a specific matrix from a MATLAB file. + /// /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. public static Matrix ReadMatrix(string filePath, string matrixName = null) where TDataType : struct, IEquatable, IFormattable @@ -71,17 +96,32 @@ namespace MathNet.Numerics.Data.Matlab } } + /// + /// Read the matrix of a MATLAB matrix data object. + /// + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix ReadMatrix(MatlabMatrix matrixData) + where TDataType : struct, IEquatable, IFormattable + { + return Parser.ReadMatrixBlock(matrixData.Data); + } + + /// + /// Read all matrices or those with matching name from a MATLAB file stream. + /// /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. public static Dictionary> ReadMatrices(Stream stream, params string[] matrixNames) where TDataType : struct, IEquatable, IFormattable { var names = new HashSet(matrixNames); - return Parser.ParseAll(stream) .Where(m => names.Count == 0 || names.Contains(m.Name)) - .ToDictionary(m => m.Name, m => m.Read()); + .ToDictionary(m => m.Name, m => Parser.ReadMatrixBlock(m.Data)); } + /// + /// Read all matrices or those with matching name from a MATLAB file. + /// /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. public static Dictionary> ReadMatrices(string filePath, params string[] matrixNames) where TDataType : struct, IEquatable, IFormattable