【问题标题】:How to load multiple .mat files into a python script如何将多个 .mat 文件加载到 python 脚本中
【发布时间】:2020-02-03 17:35:06
【问题描述】:

我想将 38 个 .mat 文件加载到字典中以保存它们。 .mat 文件被命名为 subject1 到 subject38 我试过的代码是一个简单的for循环

    import scipy.io as sio
    data = {}
    for i in range(1, 38):
        data["data{}".format(i)] = sio.loadmat('subject{}.mat'.format(i))

我得到的错误是:

Traceback(最近一次调用最后一次): 文件“D:/senior project/python/dataAqu.py”,第 7 行,在 数据[“数据{0}”.format(i)] = sio.loadmat('subject{0}.mat'.format(i)) 文件“C:\Users\mamdo\AppData\Roaming\Python\Python27\site-packages\scipy\io\matlab\mio.py”,第 208 行,在 loadmat matfile_dict = MR.get_variables(variable_names) 文件“C:\Users\mamdo\AppData\Roaming\Python\Python27\site-packages\scipy\io\matlab\mio5.py”,第 292 行,在 get_variables res = self.read_var_array(hdr, 进程) 文件“C:\Users\mamdo\AppData\Roaming\Python\Python27\site-packages\scipy\io\matlab\mio5.py”,第 252 行,在 read_var_array return self._matrix_reader.array_from_header(header, process) 文件“mio5_utils.pyx”,第 675 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header 文件“mio5_utils.pyx”,第 705 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header 文件“mio5_utils.pyx”,第 778 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_real_complex scipy.io.matlab.mio5_utils.VarReader5.read_numeric 中的文件“mio5_utils.pyx”,第 450 行 scipy.io.matlab.mio5_utils.VarReader5.read_element 中的文件“mio5_utils.pyx”,第 355 行 scipy.io.matlab.streams.ZlibInputStream.read_string 中的文件“streams.pyx”,第 194 行 文件“pyalloc.pxd”,第 9 行,在 scipy.io.matlab.pyalloc.pyalloc_v 内存错误

【问题讨论】:

  • 对循环的注释:要加载后缀为 1...38 的矩阵,循环应为 for i in range(1,39):。关于错误信息:.mat 文件有多大?
  • 每个 .mat 文件大约为 250 MB。我重新启动了我的电脑,这个错误停止发生,但现在由于文件大小,python 脚本遇到了内存问题。
  • 为什么必须同时加载它们?您可以一次循环浏览它们吗?
  • 我更改了代码以在循环中加载 mat 文件,因此 mat 文件不会一次全部加载并且可以正常工作。谢谢

标签: python scipy


【解决方案1】:

所以我发现了问题。如果出现错误,则不应由任何其他程序(如 matlab)打开 mat 文件重新启动计算机。

此外,如果存在内存问题,请尝试单独集成 mat 文件并执行您需要的任何代码,然后加载下一个文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2012-01-02
    • 2015-09-09
    • 1970-01-01
    • 2017-07-13
    • 2017-01-02
    • 2017-10-14
    相关资源
    最近更新 更多