// Copyright 2017-2018 Alexander Luzgarev using System; using System.Linq; namespace MatFileHandler { /// /// A better interface for using table objects. /// public class TableAdapter { private readonly IMatObject matObject; /// /// Initializes a new instance of the class. /// /// Source table object. public TableAdapter(IArray array) { matObject = array as IMatObject; if (matObject?.ClassName != "table") { throw new ArgumentException("The object provided is not a table."); } var cellArray = matObject["varnames"] as ICellArray; VariableNames = Enumerable .Range(0, cellArray.Count) .Select(i => (cellArray[i] as ICharArray).String) .ToArray(); NumberOfVariables = VariableNames.Length; var props = matObject["props"] as IStructureArray; Description = (props["Description"] as ICharArray).String; NumberOfRows = (int)matObject["nrows"].ConvertToDoubleArray()[0]; var rowNamesArrays = matObject["rownames"] as ICellArray; RowNames = Enumerable .Range(0, rowNamesArrays.Count) .Select(i => (cellArray[i] as ICharArray).String) .ToArray(); } /// /// Gets table description. /// public string Description { get; } /// /// Gets the number of rows in the table. /// public int NumberOfRows { get; } /// /// Gets the number of variables in the table. /// public int NumberOfVariables { get; } /// /// Gets row names. /// public string[] RowNames { get; } /// /// Gets variable names. /// public string[] VariableNames { get; } /// /// Gets all the data for a given variable /// /// Variable name. /// All data associated with the variable. public IArray this[string variableName] { get { var maybeIndex = Enumerable.Range(0, VariableNames.Length) .Where(i => VariableNames[i] == variableName) .Select(i => (int?)i) .FirstOrDefault(); if (!(maybeIndex is int index)) { throw new IndexOutOfRangeException($"Variable '{variableName}' not found."); } var data = matObject["data"] as ICellArray; return data[index]; } } } }