MATLAB Answers

Is it possible to read/write MAT files from a C# application?

185 views (last 30 days)
I would like to develop a C# application that is able to import and export MAT files compatible with MATLAB 7.14 (R2012a).

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 6 Apr 2020
Edited: MathWorks Support Team on 6 Apr 2020
MAT-file APIs are only available for Fortran and the C/C++ languages as explained on the following page:
So one option here could be to develop a wrapper .NET Assembly around these interfaces using managed C++. On the one end this could make use of the C/C++ MAT-file API and on the other end it could provide a .NET interface which you could easily access from C# code.
One workaround where you do not make use of the MAT-file API is to access MAT files using the Builder NE toolbox to build a .NET assembly.
The idea is to write some simple MATLAB functions that load a given MAT file into the workspace, then return the data. Then you can deploy these functions using Builder NE into a .NET assembly.
In your C# code, you can then reference the deployed .NET assembly and call your written functions, which will return the data back as MWArray objects in your C# code. You can manipulate the MWArray objects to obtain native arrays and other data.
The MWArray class reference is included below:
An alternative workaround is writing C# code which creates and reads MAT-files on a binary level. To do this, please refer to the following MAT file format document:
The following page is an example of a C# implementation from a third party user:
Note that this library is not developed or supported by MathWorks and if you need assistance using it, please contact the author directly.

  1 Comment

Steve Eggers
Steve Eggers on 8 May 2017
Note: CSMatIO only supports v5 .MAT file format so be sure to save your files accordingly.
CSMatIO has been moved from http://www.mathworks.com/matlabcentral/fileexchange/16319-csmatio-mat-file-io-api-for-net-2-0 to https://sourceforge.net/projects/csmatio/. A GitHub repository also exists for the same code <https://github.com/AlienEngineer/CSMatIO here > but last I checked it was not current.
Another options is https://github.com/tbeu/matio

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2009b