3.71429

3.7 | 7 ratings Rate this file 36 Downloads (last 30 days) File Size: 712.01 KB File ID: #16319

CSMatIO: MAT-file I/O API for .NET 2.0

by David Zier

 

10 Sep 2007 (Updated 11 Sep 2007)

Package contains Matlab's MAT-File I/O API for .NET 2.0. Supports Matlab 5 MAT-file format.

| Watch this File

File Information
Description

CSMatIO a .NET Library is a Matlab MAT-File I/O API for Microsoft's .NET 2.0 Architecture written entirely in C#. CSMatIO has the ability to read, write, and manipulate binary Level 5 MAT-Files.

Currently supported data types:
+ Double array
+ Single array
+ Char array
+ Structure
+ Cell array
+ Sparse array
+ Int8 array
+ UInt8 array
+ Int16 array
+ UInt16 array
+ Int32 array
+ UInt32 array
+ Int64 array
+ UInt64 array

Note:
The zlib.net.dll file is used to compress and decompress MAT-file data.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
JMatIO - Matlab's MAT-file I/O in JAVA

MATLAB release MATLAB 6.5 (R13)
Other requirements + .NET 2.0 (or later) Architecture to run assembly + Any application create with CSMatIO must include _both_ the csmatio.dll and the zlib.net.dll + Uses Visual Studio .NET 2005 for editing.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
30 Oct 2007 Steffen S

A bit tricky to get into. But once you got it its easy to use. Works stable.

24 Jun 2008 Pierluigi Fumi  
07 Jul 2008 M D

Missing cases MLArray.mxUINT16_CLASS, MLArray.mxINT16_CLASS, MLArray.mxUINT32_CLASS & MLArray.mxINT32_CLASS in the method ReadMatrix of MatFileReader class

28 Feb 2009 Mike

Writes to binary memory stream which runs out of memory at ~270 MB (.NET managed heap limitation, I believe) before being written to disk. Would be nice to have the option to take a performance hit but stream directly to disk, enabling creation of larger files.

08 Jan 2010 Lalit Parashar

Any plans to upgrade it to manipulate binary Level 7 MAT-Files.

20 May 2011 Bart Ribbens

Does there exist a binary Level 7 Mat fileformat?

In the latest document (MAT-File Format
Version 7) they still use the Level 5 binary.

23 May 2011 Jörgen Ejr973jr

Support for enumerations?

Any plans to add support for enumerations using declared as:
classdef(Enumeration) AdapRequestEnum < int32 ...

I have not found any description of the internal representation in MAT file format.

08 Sep 2011 Markus W

Easy to use an very helpful

Writing empty string seems to result in exception. Any workaround?

30 Sep 2011 Tobias Otto-Adamczak

Really useful library. However it has some bugs (struct field access is buggy, writing empty strings doesn't work) und is somehow incomplete (unsupported data types). I try to provide some fixes in the next days.

26 Oct 2011 Qirong

How do I load data using this library? I followed the test application and it only uses the function MatFileReader.ContentToString() to access the content. Is that the only way I can access the data? And then parse the string to get the real data?

28 Oct 2011 Tobias Otto-Adamczak

First, you might want to get a reference to your matlab variable:

var mfr = new MatFileReader(fileName);
MLArray mlArrayRetrieved = mfr.GetMLArray("my_array");

Second, you need to find out of which type mlArrayRetrieved really is (as MLArray is only a base class) and you cast into the real type. Depending on the real type there are different Methods/Properties that represent the data.

HTH, Tobias

28 Oct 2011 farid

hi all.
I'm trying to read a mat file but i receive this error

==========================
Error in reading MAT-file 'initialize\matfile-1.mat':
csmatio.io.MatlabIOException: Could not decompress data: zlib.ZStreamException: inflating:
   at zlib.ZOutputStream.Write(Byte[] b1, Int32 off, Int32 len)
   at csmatio.io.MatFileReader.Inflate(Stream buf, Int32 numOfBytes)
   at csmatio.io.MatFileReader.Inflate(Stream buf, Int32 numOfBytes)
   at csmatio.io.MatFileReader.ReadData(Stream buf)
   at csmatio.io.MatFileReader..ctor(String fileName, MatFileFilter filter)
==========================

what is the cause of this error message?
pleas help .... the situation is urgent!

28 Oct 2011 Qirong

So I know that the data is double array, how can I cast the MLArray type into MLDouble type? I tried

MatFileReader mfr = new MatFileReader(currentName);
            foreach (MLArray mla in mfr.Data)
            {
                MLDouble da = mla;
            }

But apparently this doesn't work...

01 Nov 2011 Tobias Otto-Adamczak

Hi farid, csmatio uses zlib to compress/decompress matlab file data. There are several possibilities why zlib throws this exception; for example you might think of
(a) file format not supported (csmatio does not support all kinds of mat files)
(b) corrupt file content
(c) bug in csmatio
...

01 Nov 2011 Tobias Otto-Adamczak

Hi Qirong, I give you a very simple but more complete example. I tested this with Matlab R2006b and Visual Studio 2005.

Consider you created a "mydata.mat" file containing a 1x10 double matrix using matlab like this:

>> squares = [1:10].^2
>> save('mydata.mat', 'squares')

You can then use the following C# code to get this data into a .NET double-Array:

namespace csmatio_test
{
    using System;
    using csmatio.io;
    using csmatio.types;

    class Program
    {
        // this array receives the matlab data
        static double[] squares;

        static void Main(string[] args)
        {
            // create a reader for the file
            MatFileReader mfr = new MatFileReader("mydata.mat");

            // get a reference to out matlab 'squares' double matrix
            MLDouble mlSquares = (mfr.Content["squares"] as MLDouble);
            if (mlSquares != null)
            {
                // now get the double values
                double[][] tmp = mlSquares.GetArray();
                squares = tmp[0];
            }
        }
    }
}

HTH, Tobias

07 Dec 2011 Markus W

Had problems reading mat-files with 2-dim arrays with larger number of elements. Saving the file with -v6 option solved the problem (so far).

28 Feb 2012 diiiego83

ok good work David.
Ther's an error if you try to open a mat file with data of "single" class.
You forget the single case in MatFileReader.cs method ReadMatrix (Add this)

case MLArray.mxSINGLE_CLASS:
                    mlArray = new MLSingle(name, dims, type, attributes);
                    //read real
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).RealByteBuffer,
                    (ByteStorageSupport)mlArray);

                    // read complex
                    if (mlArray.IsComplex)
                    {
                    tag = new ISMatTag(buf);
                    tag.ReadToByteBuffer(((MLNumericArray<double>)mlArray).ImaginaryByteBuffer,
                    (ByteStorageSupport)mlArray);
                    }
                    break;

The keys in the internal structure has been saved with "\0". For Example in MlStructure.cs method ContentToString change :
sb.Append("\t" + key + " : " + this[key].ContentToString() + "\n");
with :
sb.Append("\t" + key.Replace("\0","") + " : " + this[key].ContentToString() + "\n");

;)

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
external interface David Zier 22 Oct 2008 09:26:27
mat David Zier 22 Oct 2008 09:26:27
matfile David Zier 22 Oct 2008 09:26:27
io David Zier 22 Oct 2008 09:26:27
api David Zier 22 Oct 2008 09:26:27
net David Zier 22 Oct 2008 09:26:27
c David Zier 22 Oct 2008 09:26:27
net Klaus Schlüter 01 Dec 2010 03:12:29
net Julio 11 May 2011 06:41:20

Contact us at files@mathworks.com