CsvComplexMatrixSerializerDeserialize Method (TextReader) |
Namespace: Novacta.Analytics
Exception | Condition |
---|---|
ArgumentNullException | reader is null. |
InvalidOperationException | An error occurred during deserialization. The original exception is available using the InnerException property. |
This method reads CSV files created by method Serialize(TextWriter, ComplexMatrix) or one of its overloaded versions.
In the following example, the content of a well formatted CSV document is deserialized to create a matrix instance.
using System; using System.IO; namespace Novacta.Analytics.CodeExamples { public class CsvSerializeExample0 { public void Main() { // Create the CSV representation of a dense matrix. string[] data = new string[5] { "Dense|Complex,3,2,MatrixName", ",column0,column1", "row0,(1.0 -2.0),(NaN NaN)", "row1,(2.0 -3.0),(4.0 -1.0)", "row2,(0 0),(5.0 7.0)" }; // Show the matrix CSV representation. Console.WriteLine("CSV representation of the matrix:"); for (int i = 0; i < data.Length; i++) { Console.WriteLine(data[i]); } Console.WriteLine(); // Create a stream containing the CSV content. MemoryStream stream = new(); StreamWriter writer = new(stream); for (int i = 0; i < data.Length; i++) { writer.WriteLine(data[i].ToCharArray()); writer.Flush(); } stream.Position = 0; // Create a reader for the stream. StreamReader reader = new(stream); // Deserialize the CSV document contained in the stream. ComplexMatrix matrix = CsvComplexMatrixSerializer.Deserialize(reader); // Show the matrix. Console.WriteLine("Deserialized matrix:"); Console.WriteLine(matrix); } } } // Executing method Main() produces the following output: // // CSV representation of the matrix: // Dense|Complex,3,2,MatrixName // ,column0,column1 // row0,(1.0 -2.0),(NaN NaN) // row1,(2.0 -3.0),(4.0 -1.0) // row2,(0 0),(5.0 7.0) // // Deserialized matrix: // [column0] [column1] // [row0] ( 1, -2) ( NaN, NaN) // [row1] ( 2, -3) ( 4, -1) // [row2] ( 0, 0) ( 5, 7) //