Writing data to a .mat file

603 Views Asked by At

I am trying to write some data that I extracted from an excel file to a '.mat' file. So far, I have converted the extracted data into an array and converted this array to a dictionary before writing to a .mat file. While the conversions to the array and dictionary seem fine, when I create and write to a .mat file, the data seems corrupted. Here is my code:

import pandas as pd

file_location = '/Users/manmohidake/GoogleDrive/Post_doc/Trial_analysis/1_IndoorOutdoor.xlsx'

mydata = pd.read_excel(file_location,na_values = "Missing", sheet_name='Sheet1', skiprows = 1, usecols="F,K,Q")

import numpy

#Convert data to array
array = mydata.to_numpy()

import scipy.io

import os

destination_folder_path = '/Users/manmohidake/Google Drive/Post_doc/Trial_analysis/'

scipy.io.savemat(os.path.join(destination_folder_path,'trial1.mat'), {'array':array})

I don't really know what's gone wrong. When I open the .mat file, it. looks like this

Matlab file

1

There are 1 best solutions below

0
hpaulj On
In [1]: from scipy import io
In [2]: arr = np.arange(12).reshape(4,3)
In [3]: io.savemat('test.mat',{'array':arr})
In [4]: io.loadmat('test.mat')
Out[4]: 
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Mon Sep 20 11:36:48 2021',
 '__version__': '1.0',
 '__globals__': [],
 'array': array([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])}

In Octave

>> cd mypy
>> load test.mat
>> array
array =

   0   1   2
   3   4   5
   6   7   8
   9  10  11