mahalex / MatFileHandler

MATLAB .mat-file reading/writing API for .NET Standard
MIT License
40 stars 12 forks source link

matFileHandler #12

Closed luomeng007 closed 3 years ago

luomeng007 commented 4 years ago

I have a Question About matFileHandler. I do not know exactly how to use this package to get each Element in a 3D Array. I can not understand the documentation. For example I want to get an Array named „A“ in a mat file named data1.mat. What should I do for that? Could someone share me with an example?

mahalex commented 3 years ago

Hi, first, you read your file:

using (var stream = File.OpenRead("data1.mat"))
{
    var reader = new MatFileReader(stream);
    var matFile = reader.Read();
}

Then you can use the matFile to access data inside. You can inspect it using VisualStudio debugger or similar tools. For example, if you know that this file has a variable named "A" which is a 3d numerical array, you can say

var array = matFile["A"].Value as IArrayOf<double>;

and then access array's elements like this:

var value = array[3, 4, 2];

For more examples, you can look at tests in https://github.com/mahalex/MatFileHandler/blob/master/MatFileHandler.Tests/MatFileReaderTests.cs