0

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

MoD
  • 1
  • 1
  • Tell us about `array` - `shape`, `dtype`. I can't view your matlab image, so have no idea what's wrong.. – hpaulj Sep 20 '21 at 13:32
  • Ah I see, my apologies, I am very new to python and stack overflow. For array, shape is (10949, 3) and dtype is int64. The matlab image shows some letters and symbols that are nowhere in the array or dictionary. – MoD Sep 20 '21 at 14:09
  • I can demonstrate such a save with my own `array`, e.g `np.arange(12).reshape(4,3)`, but I can't diagnose your file. – hpaulj Sep 20 '21 at 16:09

1 Answers1

0
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
hpaulj
  • 221,503
  • 14
  • 230
  • 353