1

I am trying to convert a .mat file to csv using python. The code I am using is

import scipy.io
import numpy as np

data = scipy.io.loadmat("wiki.mat")

for i in data:
    if '__' not in i and 'readme' not in i:
        np.savetxt(("file.csv"),data[i],delimiter=',')

When ever I run this code, I get error as follows:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    np.savetxt(("file.csv"),data[i],delimiter=',')
  File "/Library/Python/2.7/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('[('dob', 'O'), ('photo_taken', 'O'), ('full_path', 'O'), ('gender', 'O'), ('name', 'O'), ('face_location', 'O'), ('face_score', 'O'), ('second_face_score', 'O')]') and format specifier ('%.18e')

I am trying to convert the .mat file from this link: https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_meta.tar

Please help me out with some working solution!

2
  • Python allows interactive processing. Why don't you control the content of data? Commented Oct 19, 2017 at 9:28
  • Sorry, I am new to python as well as matlab, I am working on something related to machine learning using java and node.js ad require some related data and the data is in .mat file. So I just want the data from this file. Commented Oct 19, 2017 at 11:49

2 Answers 2

1

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.savetxt.html

Save an array to a text file.

You can, unfortunately, only store a single numeric numpy array in a single file. Whereas your .mat file contains a structure:

>> fieldnames(imdb)
                        ans = 
                        {
                          [1,1] = dob
                          [2,1] = photo_taken
                          [3,1] = full_path
                          [4,1] = gender
                          [5,1] = name
                          [6,1] = face_location
                          [7,1] = face_score
                          [8,1] = second_face_score                          
                          [9,1] = celeb_names
                          [10,1] = celeb_id
                        }
>> imdb.name(1)        
                        ans = 
                        {
                          [1,1] = Fred Astaire
                        }

It might make sense to convert the data to a numpy dictionary (as described in "Complex matlab-like data structure in python (numpy/scipy)"), and store that as a .csv using How do I convert this list of dictionaries to a csv file? [Python]

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying, I tried to follow your steps, but I am new to python and matlab, I just know python basics. I am just looking for getting the data from the .mat file as I am working of ML and need the content to training purpose. Can you give me a working example please or some more detailed info with basics?
0

I created a package called matgrab which can be used to convert any matlab data file into a Dataframe. You can just call it like:

import matgrab
matgrab.mat2df(file.mat).to_csv(file.csv)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.