Tuesday, March 22, 2011

reading .MAT files

There are many ways to read the .mat files generated in MATLAB. I'm sure that you  might have come accross such a requirement.

Here, in this article I show you on how to retrieve the MATRIX from the .mat file in

  • JAVA (using jmatio library)
  • PYTHON (using scipy library)
First we deal with Java. We download and install jmatio library (get the jmatio.jar file and include it in the class path of java). We initialize MatFileReader class and then convert the data into java compatible datatypes.

The code is explained as follows:


In the above line where I'm accessing matfilereader.getMLArray("atest"), I can also use getArray() instead of contentToString(). I haven't done it as it looks like jmatio cannot read multiple dimension arrays (more than 2 dimensions). I tried assigning the getArray() statement to to a double array, like

double[][][] s =.....getMLArray("atest").getArray(); and it didn't work. Moreover, it said it was expecting a 2 d double array but found a 3d array.

I think we need to write it into multiple dimensions by printing it into a string and using StringTokenizer to append it in a array of multiple dimensions.

Now we deal with Python. In python its really simple with scipy installed. Go ahead and install scipy if you haven't install it(in Ubuntu give the command $sudo apt-get install python-scipy).
Now the python code:

Simple isn't it? In 3 lines of code reading a .mat file!!


  1. Is it possible to read a mat file in Android ?

  2. It should I haven't tried it. In eclipse, add the additional jars to the android project to include. Use the above code in Java normally. I would actually not recommend you to do that if you were using a large MAT file (memory constraint).

  3. I solved that problem. another major problem is, it gives me an error that cannot find mat file , while i have saved the mat file under the saem folder.

  4. If you are running windows, you might want to change the following code:

    matfilereader = new MatFileReader("./atest.mat" );


    matfilereader = new MatFileReader("atest.mat" );

    Basically I've written this on linux, so followed the MAC OSX/Linux directory structure system here.

  5. com.jmatio.io.MatlabIOException: Incorrect matlab array class: object
    at com.jmatio.io.MatFileReader.readMatrix(Unknown Source)
    at com.jmatio.io.MatFileReader.readData(Unknown Source)
    at com.jmatio.io.MatFileReader.readData(Unknown Source)
    at com.jmatio.io.MatFileReader.read(Unknown Source)
    at com.jmatio.io.MatFileReader.(Unknown Source)
    at com.jmatio.io.MatFileReader.(Unknown Source)
    at readmatfile.Main.main(Main.java:31)
    Java Result: 1

    It gives me the above error!

  6. Did you read my previous comment? Did you change the code as I've mentioned? According to your program's Exception, its clear that program is not able to find the specified mat file. That only seems to be the problem.

    Make sure that you give the entire path to the mat file you are trying to read or check my edited program above.

  7. yea i did give the entire path of the file.
    my mat file is of the type of a matlab neural network . is it because it can't read that?

  8. Your error is a little weird. I believe you are missing something trivial. Also I had trouble reading multi-dimensional MAT file through jmatio. As you've mentioned you are working on this in android. You must refer to Android Framework rules which specifies reading static files of the application that you've developed. i.e. where to place the static file in a project and how to read it. Try looking for how to read user-defined text files/images in Android. I don't think you have trouble in your program.