Manipulating .MAT Files in .NET: A Deep Dive Into CSMatIO

Written by

in

CSMatIO is a popular open-source .NET library written entirely in C# that serves as a MAT-File I/O API, allowing developers to read, write, and manipulate binary MATLAB Level 5 MAT-files directly from Microsoft .NET applications.

Originally created by David Zier and adapted from the Java-based JMatIO library, CSMatIO bypasses the need to have the official MATLAB software or COM engine installed on the client machine. It uses zlib.net (a managed ZLIB compression library) to seamlessly compress and decompress .mat file contents. Key Capabilities of CSMatIO

Pure C# Architecture: Requires only the .NET 2.0 framework (or newer), making it highly portable and lightweight for Windows-based client environments.

No MATLAB Dependency: Unlike official MathWorks COM/Engine APIs, you do not need an active MATLAB license or local installation to execute your application.

Matlab-to-.NET Data Mapping: Automatically maps data types such as MLDouble, MLCell, MLStructure, and character arrays directly to native or structured C# formats.

Level 5 MAT-file Support: Handles standard, binary Level 5 MAT-files natively. Step-by-Step Tutorial & Code Examples

To integrate CSMatIO into your C# project, you must include both the csmatio.dll and zlib.net.dll files as project references. 1. How to Read a MAT-File in C#

To read a .mat file, you initialize a MatFileReader and cast the internal MATLAB structures (MLArray, MLStructure, etc.) into their respective C# types.

using System; using System.IO; using Abramson.Matlab.IO; // Common namespace used in CSMatIO variants using CSMatIO.io; using CSMatIO.types; class Program { static void Main() { // 1. Initialize the reader with your target .mat file MatFileReader mfr = new MatFileReader(“my_data.mat”); // 2. Extract a MATLAB matrix or variable by name MLDouble mlDouble = mfr.Content[“matrixName”] as MLDouble; if (mlDouble != null) { // 3. Convert and access data as a 2D native C# array double[][] dataArray = mlDouble.GetArray(); Console.WriteLine($“Value at row 0, col 0: {dataArray[0][0]}”); } } } Use code with caution. 2. How to Write a MAT-File in C#

To export data from your C# application into MATLAB, you pack your native arrays into CSMatIO data types, add them to a list, and invoke the MatFileWriter.

using System.Collections.Generic; using CSMatIO.io; using CSMatIO.types; class Program { static void Main() { // 1. Prepare data in a standard C# jagged array double[][] data2D = new double[2][]; data2D[0] = new double[] { 1.5, 2.5, 3.5 }; data2D[1] = new double[] { 4.5, 5.5, 6.5 }; // 2. Wrap it inside a MATLAB double array object with a variable name MLDouble mlDoubleArray = new MLDouble(“exportedMatrix”, data2D); // 3. Combine variables into a list to be saved List exportList = new List(); exportList.Add(mlDoubleArray); // 4. Write the file to disk (filename, variable list, compress data flag) MatFileWriter mfw = new MatFileWriter(“output.mat”, exportList, true); } } Use code with caution. Limitations to Consider

MAT-File Versions: CSMatIO primarily supports MATLAB Level 5 formats. It does not natively support the newer v7.3 MAT-files, which are built on the HDF5 layout. If you require v7.3 files, you must look into HDF5 PInvoke libraries.

Complex Objects: Advanced MATLAB types (like custom objects, classes, or timeseries) cannot be read directly. You must first convert those objects into standard structures or matrices inside MATLAB before exporting them for CSMatIO.

Maintenance Status: The original repository is older, but functional community forks can be found on platforms like the AlienEngineer CSMatIO GitHub or SourceForge which feature bug fixes for modern .NET workloads.

If you are working on modern enterprise infrastructure with MATLAB licenses available on your server, MathWorks officially recommends using the MATLAB Engine API for .NET as an alternative.

Are you planning to build a tool that only imports data, or do you need two-way real-time communication between C# and MATLAB? Let me know so I can tailor the next steps for your architecture! CSMatIO: MAT-file I/O API for .NET 2.0 – MathWorks

Open in MATLAB Online. Reviews (13) Discussions (46) CSMatIO a . NET Library is a Matlab MAT-File I/O API for Microsoft’s . NET 2. readme.md – AlienEngineer/CSMatIO – GitHub

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *