【问题标题】:Writing data to a .mat file将数据写入 .mat 文件
【发布时间】:2021-11-14 03:18:55
【问题描述】:

我正在尝试将从 Excel 文件中提取的一些数据写入“.mat”文件。到目前为止,我已将提取的数据转换为数组,并将该数组转换为字典,然后再写入 .mat 文件。虽然对数组和字典的转换看起来不错,但当我创建并写入 .mat 文件时,数据似乎已损坏。这是我的代码:

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})

我真的不知道出了什么问题。当我打开 .mat 文件时,它。看起来像这样

Matlab file

【问题讨论】:

  • 告诉我们array - shape, dtype。我无法查看您的 matlab 图像,所以不知道出了什么问题..
  • 啊,我明白了,我很抱歉,我对 python 和堆栈溢出非常陌生。对于数组,形状为 (10949, 3),dtype 为 int64。 matlab 图像显示了一些在数组或字典中没有的字母和符号。
  • 我可以用我自己的array 演示这样的保存,例如np.arange(12).reshape(4,3),但我无法诊断您的文件。

标签: scipy mat-file


【解决方案1】:
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]])}

八度音

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

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

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2017-04-11
    • 1970-01-01
    • 2017-12-20
    • 2013-04-25
    • 2011-08-06
    • 2021-07-10
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多