问题描述:处理多个文件,每个文件出一个结果,综合这些结果,然后存成.mat文件方便matlab读取。
注:三个部分,读取文件夹中多个文件;python中list操作,将数据从python中存到matlab,每个在网上都有各自的解决方案,这里做个总结。
1. 读取文件夹中多个文件。
import os
path = ''
files = []
for file in os.walk(path):
files.append(file)
filenames = []
for dir_t in files:
if dir_t[2]:
for filename in dir_t[2]:
filenames.append(os.path.join(dir_t[0], filename))
注意:1. os.walk返回的是个三元组,第一为dirpath, 第二位子文件夹的名称,第三位为本文件夹下的文件名称。实现用的递归。
2. os.walk返回的文件顺序是乱的。
3. os.walk的详细用法请见:https://blog.csdn.net/imnisen1992/article/details/53359180。
2. 循环跑每个文件。
使用list,详情见:https://www.cnblogs.com/mpp0905/p/8186328.html。
具体为:
res=[]
for i in files:
res_t = fun()
res.append(res_t)
3.保存结果到.mat文件
import numpy, scipy.io
scipy.io.savemat('filename.mat', mdict={'res': res})
详情请见:https://blog.csdn.net/u013630349/article/details/48162303。