很难从您的帖子中看出什么是输入格式,以及您想要的输出格式是什么。
我给你一个读取文件夹中所有.mat文件的例子,以及一个如何将data['data']显示为图像的例子。
我希望这个例子足以让你自己继续前进。
我使用 MATLAB 创建了一个示例数据集 'A2001.mat'、'A2002.mat'、'A2003.mat'。
如果您安装了 MATLAB,我建议您执行以下代码来创建示例输入(以使 Python 示例可重现):
ECG.sex = 'Male';
ECG.age = 68;
data = im2double(imread('cameraman.tif')) / 10; % Divide by 10 for simulating range [0, 0.1] instead of [0, 1]
save('A2001.mat', 'ECG', 'data');
ECG.sex = 'Male';
ECG.age = 46;
data = im2double(imread('cell.tif'));
save('A2002.mat', 'ECG', 'data');
ECG.sex = 'Female';
ECG.age = 54;
data = im2double(imread('tire.tif'));
save('A2003.mat', 'ECG', 'data');
Python 代码示例执行以下操作:
- 使用
glob.glob('*.mat') 获取文件夹中所有mat 文件的列表。
- 迭代
mat 文件,从文件中加载数据,并将数据附加到列表中。
循环的结果是一个名为alldata 的列表,其中包含来自所有mat 文件的数据。
- 迭代
alldata 并将 data['data'] 显示为图像。
(假设data['data'] 是您要显示为图像的矩阵)。
代码如下:
from matplotlib import pyplot as plt
from mat4py import loadmat
import glob
import os
os.chdir('/Users/Ashi/Downloads/TrainingSet2')
# Get a list for .mat files in current folder
mat_files = glob.glob('*.mat')
# List for stroring all the data
alldata = []
# Iterate mat files
for fname in mat_files:
# Load mat file data into data.
data = loadmat(fname)
# Append data to the list
alldata.append(data)
# Iterate alldata elelemts, and show images
for data in alldata:
# Assume image is stored in matrix named data in MATLAB.
# data['data'], access data with string 'data', becuase data is a dictionary
img = data['data']
# Show data as image using matplotlib
plt.imshow(img, cmap='gray')
plt.show(block=True) # Show image with "blocking"
更新:
心电图数据不是图像,而是 12 个数据样本的列表。
数据的内部结构(data = loadmat(fname)之后)是:
- 父字典名为
data。
-
data 包含 data['ECG'] 中的字典。
-
data['ECG']['data'] 是 12 个列表的列表。
以下代码迭代mat文件并将ECG数据显示为图形:
from matplotlib import pyplot as plt
from mat4py import loadmat
import glob
import os
import numpy as np
os.chdir('/Users/Ashi/Downloads/TrainingSet2')
# Get a list for .mat files in current folder
mat_files = glob.glob('*.mat')
# List for stroring all the data
alldata = []
# Iterate mat files
for fname in mat_files:
# Load mat file data into data.
data = loadmat(fname)
# Append data to the list
alldata.append(data)
# Iterate alldata elelemts, and show images
for data in alldata:
# The internal structure of the data is a dictionary with a dictionary.
ecg = data['ECG']
data = ecg['data'] # Data is a list of lists
# Convert data to NumPy array
ecg_data = np.array(data)
# Show data as image using matplotlib
#plt.imshow(img, cmap='gray')
plt.plot(ecg_data.T) # Plot the data as graph.
plt.show(block=True) # Show image with "blocking"
结果:
A0001.mat:
A0002.mat:
带标签的图表:
# Iterate alldata elements, and show images
for data in alldata:
# The internal structure of the data is a dictionary with a dictionary.
ecg = data['ECG']
data = ecg['data'] # Data is a list of lists
# Convert data to NumPy array
#ecg_data = np.array(data)
# Show data as graph using matplotlib
# Iterate data list:
for i in range(len(data)):
# Plot the data as graph.
# Set labels d0, d1, d2...
plt.plot(data[i], label='d'+str(i))
plt.legend() # Add legend
plt.show(block=True) # Show image with "blocking"
结果: