Bluebit Software
Bluebit Software Support Forum
 Home          Members     Calendar     Who's On

Welcome Guest ( Login | Register )
        



Saving a matrix to a file and reading it back... Expand / Collapse
Message
Posted Friday, June 17, 2005 12:21 AM Post #63
 

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie
Hello,

I’m using the .NET Matrix Library. I have one question, and I can’t find the answer.

Is there a way to store a matrix on disk, and read it again from the disc back to the application? I know that I can use the .ToString method to save the matrix in a text file, but how can I then read the matrix back to my application?

I want to save the matrix on disc because I don’t want to create the matrix always from scratch.

Thank you for your answer.

Posted Friday, June 17, 2005 12:29 AM Post #224
 

Bluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit Support
Hi

It can be done very easily using either XML or Binary Serialization. Please review the following code example:


private void SaveAndLoadDemo()
{
// Create a matrix object
Matrix myMatrix = new Matrix(4, 4);
myMatrix.FillRandom();
// Print the matrix
Console.WriteLine(myMatrix);

// Save the matrix to an xml file
SaveXML(myMatrix, "C:\\MyMatrix.xml");

// Open the saved matrix into another variable
Matrix mySavedMatrix = LoadXML("C:\\MyMatrix.xml");
Console.WriteLine(mySavedMatrix);

}


// Using XML Serialization to save a matrix
private void SaveXML(Matrix matrix, string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create) ;
// Create a xml Serializer object
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Bluebit.MatrixLibrary.Matrix));

//Save the matrix
xmlSer.Serialize(fs, matrix);

// Close the file stream
fs.Close();
}

// Load a matrix from an XML file
private Matrix LoadXML(string filePath)
{
//Open the XML file
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open) ;

// First create a xml Serializer object
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Matrix));

// Deserialize the Matrix object
Matrix matrix = (Matrix) xmlSer.Deserialize(fs);

// Close the file stream
fs.Close();

// Return the matrix object
return matrix;
}

// Using Binary Serialization to save a matrix
private void SaveBinary(Matrix matrix, string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create) ;
// Initialize a BinnaryFormatter object
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFor = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

//Save the matrix
binFor.Serialize(fs, matrix);

// Close the file stream
fs.Close();
}

// Load a matrix from a binary file
private Matrix LoadBinary(string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath,System.IO.FileMode.Open) ;

// Initialize a BinnaryFormatter object
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFor = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

// Deserialize the Matrix object
Matrix matrix = (Matrix) binFor.Deserialize(fs);

// Close the file stream
fs.Close();

// Return the matrix object
return matrix;
}



Trifon Triantafillidis

Lead Developer

Bluebit Software

Posted Thursday, July 21, 2005 12:03 PM Post #237
 

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member
SaveXML(myMatrix, "C:\\MyMatrix.xml");
What is the same code to save matrix but in vb.net?

Posted Monday, July 25, 2005 1:26 AM Post #238
 

Bluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit SupportBluebit Support

guille,

here is the same code in vb.net


Private Sub SaveAndLoadDemo()
'Create a matrix object
Dim myMatrix = New Matrix(4, 4)
myMatrix.FillRandom()
'Print the matrix
Console.WriteLine(myMatrix)

'Save the matrix to an xml file
SaveXML(myMatrix, "C:\\MyMatrix.xml")

'Open the saved matrix into another variable
Dim mySavedMatrix As Matrix = LoadXML("C:\\MyMatrix.xml")

Console.WriteLine(mySavedMatrix)

End Sub


'Using XML Serialization to save a matrix
Private Sub SaveXML(ByVal matrix As Matrix, ByVal filePath As String)
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Create)
'Create a xml Serializer object
Dim xmlSer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(Bluebit.MatrixLibrary.Matrix))

'Save the matrix
xmlSer.Serialize(fs, matrix)

'Close the file stream
fs.Close()

End Sub


'Load a matrix from an XML file
Private Function LoadXML(ByVal filePath As String) As Matrix
'Open the XML file
Dim FS As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Open)

'First create a xml Serializer objecT
Dim xmlSer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(Bluebit.MatrixLibrary.Matrix))

'Deserialize the Matrix object
Dim matrix As Matrix = CType(xmlSer.Deserialize(FS), Bluebit.MatrixLibrary.Matrix)

'Close the file stream
FS.Close()

'Return the matrix object
Return matrix

End Function

'Using Binary Serialization to save a matrix
Private Sub SaveBinary(ByVal matrix As Matrix, ByVal filePath As String)
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Create)
'Initialize a BinnaryFormatter object
Dim binFor As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

'Save the matrix
binFor.Serialize(fs, matrix)

'Close the file stream
fs.Close()

End Sub

'Load a matrix from a binary file
Private Function LoadBinary(ByVal filePath As String) As Matrix
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Open)

'Initialize a BinnaryFormatter object
Dim binFor As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

'Deserialize the Matrix object
Dim matrix As Matrix = CType(binFor.Deserialize(fs), Bluebit.MatrixLibrary.Matrix)

'Close the file stream
fs.Close()

'Return the matrix object
Return matrix

End Function


Trifon Triantafillidis

Lead Developer

Bluebit Software

« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Trifon

Permissions Expand / Collapse

All times are GMT -5:00, Time now is 5:41am

Powered by InstantForum.NET v4.1.4 © 2012
Execution: 0.359. 10 queries. Compression Disabled.
.NET Matrix Library