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

读取LabVIEW生成的50×50矩阵TDMS文件时仅获部分通道的问题求助

问题:读取LabVIEW生成的TDMS矩阵文件不完整

我尝试读取一个由LabVIEW生成的TDMS文件,该文件对应50×50矩阵(共2500个元素),垂直方向变量为ROT,水平方向为L。但使用以下代码仅能读取到第一个通道ROT和最后一个通道L49,无法获取完整矩阵:

import numpy as np
pip install npTDMS
from nptdms import TdmsFile as td
import pandas as pd


def read_tdms(path):

    #Init of the dataframe 
    df=pd.DataFrame()
    tdms_file=TdmsFile.read("C:\\Users\\Venom\\Desktop\\Nueva carpeta\\1 S_05_V_P2_200430_140647.tdms")
    #Iteration loop over all groups
    for group in tdms_file.groups():
        group_name=group.name
        #Iteration loop over all channels
        for channels in group.channels():
            #Get data
            channel_name=channel.name
            properties=channel.properties
            print(f"properties of channels are: {properties}")
            print(f"channel name is: {channel_name}")
            data=channel[:]
        
            #Store the data into a Dataframe
            df[channel_name]=data
               
        
        else:
            pass
       
    return df

解决方法

1. 修正代码中的关键错误

代码存在几个明显问题,导致通道遍历不完整:

  • 变量名不匹配:循环定义了channels,但后续引用未定义的channel,会导致运行报错或仅处理部分通道
  • 错误的指令位置:pip install npTDMS是终端命令,不能写在Python代码的import块中,需提前在终端执行
  • 硬编码路径:函数参数path未被使用,直接写死文件路径降低了代码通用性

2. 修正后的完整代码

import numpy as np
from nptdms import TdmsFile
import pandas as pd


def read_tdms(path):
    # 初始化DataFrame
    df = pd.DataFrame()
    # 使用传入的路径读取TDMS文件
    tdms_file = TdmsFile.read(path)
    
    # 遍历文件中所有分组
    for group in tdms_file.groups():
        # 遍历当前分组下的所有通道
        for channel in group.channels():
            channel_name = channel.name
            properties = channel.properties
            print(f"通道属性: {properties}")
            print(f"通道名称: {channel_name}")
            # 读取通道数据
            data = channel[:]
            
            # 将数据存入DataFrame
            df[channel_name] = data
    
    return df

# 调用示例:替换为你的文件路径
file_path = "C:\\Users\\Venom\\Desktop\\Nueva carpeta\\1 S_05_V_P2_200430_140647.tdms"
result_df = read_tdms(file_path)
# 验证输出矩阵形状是否为(50, 50)
print("输出矩阵形状:", result_df.shape)

3. 额外排查步骤

如果修正后仍无法读取所有通道,可添加以下代码确认所有通道是否被识别:

# 在读取文件后添加,打印所有通道名称
all_channel_names = [ch.name for group in tdms_file.groups() for ch in group.channels()]
print("识别到的所有通道:", all_channel_names)
print("通道总数:", len(all_channel_names))

若通道总数不足50,说明TDMS文件的结构可能包含多个分组,或部分通道的命名/存储格式特殊,需进一步核对LabVIEW生成文件时的配置。


内容的提问来源于stack exchange,提问作者Nelson Barrientos Lobos

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 19:50:34