问题描述
这与我的最后一个问题有关,可以在这里找到.我正在处理类似于我在该链接中描述为markerList 的列表的列表 - 所以是一个具有三个级别的列表.我需要将此信息保存为 .mat 文件,但我无法将其保存为正确的类型.使用 scipy.io.savemat 时,它会将列表保存为 200x40x2 的单个单元格,而它应该是一组 200 个单元格,每个单元格包含一个 40x2 单元格.
This is relating to my last question, which can be found here. I'm dealing with lists similar to the list I describe in that link as markerList - so a list with three levels. I need to save this information as a .mat file, but I can't get it to save in the right type. When using scipy.io.savemat, it saves the list as a 200x40x2 single, when it should be a set of 200 cells, each containing a 40x2 cell.
我用来保存的代码是:
matdict = dict(markers = (markerList), sorted = (finalStack))
scipy.io.savemat('C:\pathname\\sortedMarkers.mat', matdict)
令我困惑的是,它以正确的格式保存了markerList(1x200 单元格,每个单元格大小不同),而不是finalStack(保存为200 x 40 x 2 的单个单元格).最重要的是,在我弄清楚这段代码的其余部分之前,它会正确保存 finalStack - 这让我认为当它保存的数据大小不统一时,它可能会保存为一个单元格.(finalStack 的大小是统一的;markerList 不是.)
What is confusing to me is that it saves markerList in the correct format (1x200 cell, each a cell of varying size), but not finalStack (saved as a 200 x 40 x 2 single). On top of that, before I had figured out the rest of this code, it would save finalStack correctly - which makes me think that perhaps it saves as a cell when the data it is saving isn't uniform in size. (finalStack is uniform in size; markerList is not.)
有没有办法将像这样复杂的数据结构保存为 .mat 文件?
Is there a way to save a complicated data structure like this as a .mat file?
推荐答案
根据 savemat 文档,转换为 'objects' 的 numpy 数组:
As per savemat documentation, convert into a numpy array of 'objects':
from scipy.io import savemat
import numpy
a = numpy.array([[1,2,3],[1,2,3]])
b = numpy.array([[2,3,4],[2,3,4]])
c = numpy.array([[3,4,5],[3,4,5]])
L = [a,b,c]
FrameStack = numpy.empty((len(L),), dtype=numpy.object)
for i in range(len(L)):
FrameStack[i] = L[i]
savemat("myfile.mat", {"FrameStack":FrameStack})
八度:
>> load myfile.mat
>> whos FrameStack
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
FrameStack 1x3 144 cell
Total is 3 elements using 144 bytes
>> whos FrameStack{1}
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
FrameStack{1} 2x3 48 int64
Total is 6 elements using 48 bytes
这篇关于使用 scipy.io.savemat 保存嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!