From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Updated TDMS importer to Matlab 2012

Code and Documents

Attachment

Overview

Within the zip file is an updated version of the TDMS viewer example that was produced by NI a few years ago. The update includes; importing timestamp data, importing multiple user properties, importing multiple groups/channels and this is all done through a function 'Read_TDMS.m' that exports a single structure to your matlab workspace.

Also included is a LV2013 library for setting up TDMS files with the basic properties, author, datetime, description and title.

Description

The function 'Read_TDMS.m' can be used to import large TDMS files into matlab for processing and analysis. An example has been included called 'Example_read_TDMS_func.m'. Within this script the function is called with no extra file properties on a 64-bit machine. The function has a singular output of a structure and three argument inputs; Read_TDMS(sysBit,addFProps,addFPropsName) sysBit is an integer either 32 or 64 depending on your system specification (this loads the different .dll and .h files for NI components to work), addFProps is an integer corresponding to how many additional properties that have been added and addFPropsName is a cell array of the names of the additional properties that the user has added.

It is assumed that waveforms are being saved in this explanation although it is possible to save other forms of data. In the example there are three methods of plotting data; timeseries, dt from timestamp and presumed dt. As is mentioned in the comments, timeseries is only usable, reliably, when the timeperiod is over 1 second (so pretty useless for most TDMS applications unless data is taken over many days). The dt from timestamp is taken by converting the timestamp data into seconds and to use t(1) as 0 to find dt. This will be in error if the day changes as it only looks at the hours, minutes and seconds. However this could be by the user if required. The final method of finding the time is to be used when timestamp data isn't changed. It assumes that the spacing between samples is constant (which it may not be without realtime systems).

The code did occasionally cause matlab to crash but a simple restart of the program fixes this (not sure of the cause). It has been more stable recently but should a crash occur try re running the code. If it keeps occuring please let me know with as much information of what you were doing as possible so I can attempt a fix.

The Labview code is designed to fit into other VIs that you have written. There are VIs for single and multiple groups as there is slightly different coding required for each.

File definition sets up the general properties by using a config.ini to read the file name format that the user requires, the default is set to the date and time. There is then the option of adding a suffix within a pop up window making the default file name "%Y%m%d_%H%M%S_%suffix.tdms". Within the dialog box there is also a comment section and an author box, these will be saved to the properties of description and author within the TDMS file. The suffix is saved to the title property and the current date time is saved to the datetime property. There is a NI_MinimumBufferSize property saved for each of the channels as well, this is due to some memory leakage occuring when we tested the code without it. If false is inputted into the 'Record data?' input to this VI then a file called "dud.tdms" will be saved to the desktop.

Log takes a variable input but is defaulted to 1D array of waveforms in the example, but this could be changed to doubles or anything that you want to save to a TDMS file.

The TDMS files need to be closed after use in order to save correctly which isn't in the library.

If you find any bugs or suggest anyways of making the code more efficient then please let me know. I'm relatively new to Matlab and still consider myself a novice with Labview.

Steps to Implement Code

Matlab code:

  1. To call the function have 'Read_TDMS.m' in your matlab active folder with your calling script
  2. Call using the following command 'TDMS = Read_TDMS(64,0,{})' for no extra properties on a 64 bit machine. TDMS = Read_TDMS(32,2{Ex1, Ex2}) would be two additional properties called Ex1 and Ex2 on a 32 bit machine.
  3. TDMS.file contains the properties of your TDMS file
  4. TDMS.groupname.channelname contains the data, dt, unit, samples and start_time properties of waveform information or just the data if not waveform.
  5. TDMS.groupname.channelname_t contains any timestamp data that has been saved.

Labview code:

  1. Decide how many groups you are wanting to implement, use either single group VIs or multiple group VIs for the following from 'Logging.lvlib'
  2. Initialise the TDMS file with 'File Definition - ....vi' input group names, channel names and whether you will be logging or not.
  3. Have 'Log - ....vi' within a while loop with data inputted into it and 'Record data?' enabled to save to the TDMS created by File definition.
  4. Put a 'TDMS Close.vi' from File I/O>TDMS Streaming on the block diagram outside of the while loop.
  5. Files are saved into a new folder called 'Log' within the application directory.

Requirements

Software

Matlab (tested on 2012B)

LabVIEW 2013 (ask for previous versions)

Hardware (optional)

NI DAQ for waveform acquisition

Additional Images or Video

A basic layout (with no data input)

basic layout.PNG

File definition - single group.vi

file def.PNG


Pete
Systems engineer (CLAD LV2013)

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
jootv
Member
Member
on

Hello,

When I read TDMS-files containing temperature values from a type-T element the values in Matlab looks realy strange. A temperature of 22C is in Matlab -1.208e8. Voltage signals looks okay, but the temperature signals seems to gets the wrong scaling.

Any idea why?

/Jonas

Contributors