python变量保存为.mat文件

本文主要介绍python中的变量(list, dict, ndarray 类型)怎么保存为.mat文件( matlab 使用)

方法:使用scipy库中的scipy.io.savemat(savePath, maydict={…})函数

import scipy.io
import ...
...
...
...
#假设你要保存的变量为 bData and aData
#将bData 和 aData保存到result.mat中
scipy.io.savemat('result.mat', mdict={'bData': bData, 'aData': aData,})
#此时result.mat包含一个cell,内容是名为'bData'和'aData'的struct

python中的变量(list , dict , ndarray类型)保存为.mat文件时,变量类型的对应关系

  1. list —>cell (list内容为 dict 或 ndarray)
  2. dict—>struct 若dict内容为list,且每个list长度不一,那么保存时会发生错误,为了避免该类错误,需要将list转换为ndarray。
  3. list—>mat矩阵(list内容为字符串或数值)a.若list内容为字符串+数值或都为字符串,在保存时,将数值型数据转换为字符串,a1.若list第一个元素为数值型数据,此时mat矩阵的列数为list[i]最长的长度(若list[i]最长的长度小于11则mat矩阵的列数为11),其他长度不足的补空格,所以一般都将list转换为ndarray再保存;a2.若list第一个元素为字符串,此时mat矩阵的列数为list[i]最长的长度,其他长度不足的补空格。b.若list内容为纯数值,则保存的mat矩阵元素为数值。对于a1情况会报警告: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
    arr[empties] = ’ ’
  4. ndarray—>mat矩阵或cell(ndarray元素数据类型不同)。

注:

  • list(a变量)转ndarray(b变量)方法

若list元素数据类型一样:b = numpy.array(a)
若list元素数据类型不一样:b=numpy.array(a, dtype=object)

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值