matlab 到 python:获取 matlab 元胞数组值
我对 matlab 和 python 非常陌生,需要在 python 中使用 matlab 元胞数组中的一些值。
我有一个整数元胞数组,在执行这行代码并打印结果后,我得到:
a = loadmat('file.mat')
print a
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Sep 21 15:30:15 2011', 'juncForward_c': array([[ [[ [[ ([[30]], **[[91], [87]]**, [[3]], [[2.2455372690184494, 3.6052402625905993, 5.5884470309828833]], [[14.0, 4.0, 15.4]], [[-1, -1, 2]], [[91, 89, 93], [88, 85, 86]], [[500, 500, 5]], [[1, 2, 3]], [[11.133333333333333]], **[[4]]**, [[1]], [[1]], [[1.0], [20.365168528421695]])]]
[[ ([[30]], **[[99], [80]]**, [[3]], [[4.0376480381611373, 2.3561944901923448, 6.0857897473297058]], [[10.0, 15.4, 16.600000000000001]], [[-1, 1, 3]], [[98, 98, 100], [79, 81, 80]], [[500, 6, 33]], [[1, 2, 3]], **[[14]]**, [[2]], [[1]], [[1]], [[2.0], [6.6573267908372973]])]]
并且打印输出继续。
有人可以向我解释一下单元阵列是如何排列的吗? (那么上面是多少维?)
那么我有几个问题: 1) 在这个元胞数组中,有变量“标签”和“坐标”被分配给一个数组/元胞(我不知道哪个是实际使用的术语)——这些值以粗体显示。我想用 Python 编写来访问这些值。我该怎么办?
2) 为了测试,我尝试了这个 -> juncInfoF = a.get('juncForward_c')
当我尝试打印 juncInfoF 时,它打印“None”。为什么会这样呢?我很困惑,因为以前当我尝试这个时,它有效。我什至可以做到这一点 -> 打印juncInfo[0][9][0][0]
。但现在我什至无法做到上述任何一项。
I am very new to matlab and python and need to use some values in python from the matlab cell array.
I have a cell array of integers that after i execute this line of code and print the result, i get this:
a = loadmat('file.mat')
print a
{'__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Sep 21 15:30:15 2011', 'juncForward_c': array([[ [[ [[ ([[30]], **[[91], [87]]**, [[3]], [[2.2455372690184494, 3.6052402625905993, 5.5884470309828833]], [[14.0, 4.0, 15.4]], [[-1, -1, 2]], [[91, 89, 93], [88, 85, 86]], [[500, 500, 5]], [[1, 2, 3]], [[11.133333333333333]], **[[4]]**, [[1]], [[1]], [[1.0], [20.365168528421695]])]]
[[ ([[30]], **[[99], [80]]**, [[3]], [[4.0376480381611373, 2.3561944901923448, 6.0857897473297058]], [[10.0, 15.4, 16.600000000000001]], [[-1, 1, 3]], [[98, 98, 100], [79, 81, 80]], [[500, 6, 33]], [[1, 2, 3]], **[[14]]**, [[2]], [[1]], [[1]], [[2.0], [6.6573267908372973]])]]
and the print out continues on.
Could someone explain to me how the cell array is arranged? (so how many dimensions is the above?)
I then have a few questions:
1)
Within this cell array, there are variables 'label' and 'coordinates' that are assigned to one array/cell (i dont know which is the actual term to use) -- the values in bold. I would like to write in Python to access this values. How should I go about it?
2)
To test, I tried this -> juncInfoF = a.get('juncForward_c')
and when i try to print juncInfoF, it prints 'None'. Why is that so? I am puzzled because previously when i tried this, it works. I could even do this -> print juncInfo[0][9][0][0]
. but now I cannot even do any of the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想
loadmat('file.mat')
返回 shelve 的实例-类,继承自 dict 类。我假设您正在使用 scipy.io 的 loadmat 函数?scipy doc 仅提到 loadmat() 返回字典,但我的猜测是,示例中返回的对象
a
是一个打开的搁置文件,当a
为时,您无法再访问其内容已关闭,这可能除了手动调用
a.close()
之外,发生这种情况还有多种原因。为了防止这种情况,请在加载此数据时首先将所有数据复制到真实的字典中:
现在您可以从
mat_dict
检索所有键和值:请注意,只要您正在测试和验证,这就是一个有效的解决方案弄清楚 mat_dict 数据的含义。在你的脚本/模型/函数中,你通常会只加载你想要的 shelve dict 的部分,然后显式关闭文件:
我不知道 with 语句是否有效(shelve 还不支持它),你必须测试它,但即使在加载
a
期间引发异常,它也应该处理关闭:edit: subclass from numpy array to add extra attribute
所以现在如果你想要你的数组类似于matlab结构,你做:
等等
I imagine
loadmat('file.mat')
returns an instance of a shelve-class, that is inherited from the dict class. I assume you are using the loadmat function from scipy.io?The scipy doc only mentions loadmat() returns a dictionary, but my guess is that the returned object
a
in your example is an open shelve-file, and you can no longer access its content whena
isclosed
, which might happen for several reasons apart from callinga.close()
mannually.To prevent this, copy all data into a real dictionary first, while loading this data:
Now you can retrieve all keys and values from
mat_dict
:note that this is a valid solution as long as you are testing and figuring out what the mat_dict data is all about. In your script/model/function you would tipically load only the part of the shelve dict that you want, and then close the file explicitly:
I don't know if the with statement works (shelve does not support it as yet), you'll have to test it, but it should handle closing even when an exception is thrown during loading of
a
:edit: subclass from numpy array to add additional attributes
so now if you want your array resembling the matlab structure, you do:
etc
如果您的 mat 文件中只有带有整数或字符串列表的元胞数组,我为此编写了一个通用代码。
例如,在 matlab 中,您可以执行类似的操作:
此处你可以找到如何在 python 中读取这个 mat 文件。
If in your mat file there are only cell arrays with list of integers or strings, I wrote a generic code for that.
For example, in matlab you can do something like that:
Here you can find how to read this mat file in python.