-2

I have a 2x8638 matrix in a .mat file from Matlab. When I convert to Python using the following code:

import scipy.io
mat=scipy.io.loadmat('u_x.mat')

Python creates a dictionary structure.

{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: qua jul 21 08:43:40 2021', '__version__': '1.0', '__globals__': [], 'u_x': array([[ 0.00000000e+00,  1.39250848e-07,  8.35505086e-07, ...,
         2.99900000e+01,  2.99968199e+01,  3.00000000e+01],
       [ 0.00000000e+00, -5.48522290e-11, -1.97447920e-09, ...,
         5.29180779e+00,  5.22323380e+00,  5.20265331e+00]])}

​I wanted to convert to a .csv file so that the data handling is better.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user13053456
  • 35
  • 1
  • 4
  • 1
    Well, either save it in csv (instead of mat), or write a python code that handles the dictionary and saves it to csv. But this python code will need to extract the data from the dictionary, so once you've done that, there is no reason to atually write it on csv. – Ander Biguri Jul 21 '21 at 13:15
  • When I do this in Matlab: FileData = load('u_x.mat'); csvwrite('u_x.csv', FileData); the following message appears: Check for missing argument or incorrect argument data type in call to function 'real'. Error in csvwrite (line 47) throw(e) – user13053456 Jul 21 '21 at 13:36

1 Answers1

1

As I can see, the key to your data in the loaded data is 'u_x' (Note: scipy.io loads a .mat into a python dictionary.

You could go this way:

import scipy.io
mat=scipy.io.loadmat('u_x.mat')

your_data = mat['u_x']

As I don't have your data to try with, you may need to operate a transpose on your_data then use pandas to save it to a csv file like so:

import pandas as pd
df = pd.DataFrame(your_data)
df.to_csv('your_data.csv') #your_data.csv final data in csv file
Dharman
  • 30,962
  • 25
  • 85
  • 135
arilwan
  • 3,374
  • 5
  • 26
  • 62