如何将python字典列表另存为matlab结构化数组的数组? [英] How to save a list of python dictionaries as an array of matlab structured arrays?

查看:149
本文介绍了如何将python字典列表另存为matlab结构化数组的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个要在Matlab环境中读取的文件. matlab中的结构看起来像这样

I am trying to create a file to be read in a matlab enviroment. The structure in matlab looks like this

trx(1) = 
          x: [1×1500 double]
          y: [1×1500 double]
          a: [1×1500 double]
          b: [1×1500 double]
      theta: [1×1500 double]
 firstframe: 1
   endframe: 1500
    nframes: 1500
        off: 0 


 trx(2) = 
          x: [1×751 double]
          y: [1×751 double]
          a: [1×751 double]
          b: [1×751 double]
      theta: [1×751 double]
 firstframe: 750
   endframe: 1500
    nframes: 751
        off: -749 

所以我自然地用所需的字段创建了一个python字典并创建了一个列表,然后使用savemat.但是,当我在matlab中加载时,我只会得到单元格数组.我还尝试使用,但是问题在于,并非所有字段都是具有相同形状的数组,例如"firstframe"是一个整数.然后,当我使用fromarrays()但它抱怨,因为形状不匹配.

So naturally I created a python dictionary with the required fields and create a list, then used savemat. However when I loaded in matlab I only get cell arrays. I also tried using this but the problem is that not all of the fields are arrays with the same shapes for example 'firstframe' is an int. Then when I used fromarrays() but it complains because the shape does not match.

我现在正在尝试将字典转换为结构化数组,但未找到任何相关内容.并且还尝试创建一个numpy记录,以允许数组使用不同的形状.任何灯光都非常受欢迎

I am trying now to convert a dictionary to an structured array, but have not found anything related. And also trying to create a numpy record that allows different shapes for the arrays. Any light very welcome

推荐答案

在八度音阶中

M =

  scalar structure containing the fields:

    x =

       1   2   3   4

    y =

       5   6   7   8

    one =  1
    two =

       1   2

>> save -7 struct.mat M

在Ipython中:

In [450]: dat = io.loadmat('struct.mat')
In [451]: dat
Out[451]: 
{'__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.2.2, 2019-02-08 18:49:49 UTC',
 '__version__': '1.0',
 '__globals__': [],
 'M': array([[(array([[1., 2., 3., 4.]]), array([[5., 6., 7., 8.]]), array([[1.]]), array([[1., 2.]]))]],
       dtype=[('x', 'O'), ('y', 'O'), ('one', 'O'), ('two', 'O')])}

此处M是(1,1)结构化数组,所有字段均为object dtype.这样,它们可以各自具有自己的形状.标量是(1,1)矩阵.

Here M is (1,1) structured array, with all fields being object dtype. That way they can each have their own shape. The scalar is a (1,1) matrix.

这篇关于如何将python字典列表另存为matlab结构化数组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆