You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含嵌套数组的.mat电池退化数据集转换为CSV格式?

牛津电池退化数据集.mat转CSV转换方案

核心代码实现

假设你已经通过scipy.io.loadmat加载了数据集,以下是完整的转换流程:

import pandas as pd
from scipy.io import loadmat

# 加载.mat数据集(如果还未加载)
mat_data = loadmat('oxford_battery_dataset.mat')

# 遍历Cell1到Cell8
for cell_id in range(1, 9):
    cell_key = f'Cell{cell_id}'
    # 提取当前电池的循环数据列表(scipy加载mat cell数组后需通过[0,0]获取实际内容)
    cycle_list = mat_data[cell_key][0, 0]
    
    cell_records = []
    # 处理每个循环周期的数据
    for cycle_num, cycle_data in enumerate(cycle_list, start=1):
        # 提取各字段并转为一维数组(适配DataFrame结构)
        time = cycle_data['t'][0, 0].flatten()
        voltage = cycle_data['v'][0, 0].flatten()
        capacity = cycle_data['q'][0, 0].flatten()
        temperature = cycle_data['T'][0, 0].flatten()
        
        # 拼接单循环数据为DataFrame,可选添加Cycle列区分周期
        single_cycle_df = pd.DataFrame({
            'Cycle': [cycle_num] * len(time),
            'Time': time,
            'Voltage': voltage,
            'Capacity': capacity,
            'Temperature': temperature
        })
        cell_records.append(single_cycle_df)
    
    # 合并当前电池所有循环数据
    final_cell_df = pd.concat(cell_records, ignore_index=True)
    # 导出为CSV文件
    final_cell_df.to_csv(f'{cell_key}.csv', index=False)

关键细节说明

  • MAT数据结构适配:scipy加载MATLAB的cell数组时,会返回二维numpy数组,必须通过[0,0]索引才能取出实际存储的循环数据列表。
  • 数组扁平化:MATLAB导出的字段数据多为(1, N)型二维数组,用flatten()转为一维后,才能保证各字段长度一致,顺利构建DataFrame。
  • Cycle列可选:如果不需要区分不同循环周期,只需删除字典中的'Cycle': [cycle_num] * len(time)这一行即可。
  • 异常处理(可选):若数据集中存在循环字段长度不匹配的情况,可添加判断跳过异常循环:
    # 在提取字段后添加长度校验
    if len(time) == len(voltage) == len(capacity) == len(temperature):
        cell_records.append(single_cycle_df)
    else:
        print(f"Cell{cell_id} Cycle{cycle_num} 字段长度不匹配,已跳过")
    

内容的提问来源于stack exchange,提问作者Leila Amani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:42:41