Time channel position incorrect when writing timetable to MF4

조회 수: 1 (최근 30일)
James
James 2024년 1월 16일
댓글: Shubh 2024년 1월 22일
I am trying to write data from an excel script into MF4 format to replay the data in a different script. However, the time channel generated when creating the MF4 file is always positioned as the last channel/variable in the MF4 file, but for my MF4 file to sync the time correctly it needs to be the first channel/variable.
Is there any way of specifing the position of 'time' pre or post MF4 creation?
Here is my current code:
clear
clc
frequency = 20; % Hz
TTdata = readtimetable("ExcelData.xlsx","SampleRate",frequency); % Reads the excel file using given sample frequency to sync time
info = mdfInfo("WorkingMF4.mf4")
info.Version = "4.10";
mdfCreate("NewMF4.mf4","FileInfo",info); % Generates new mf4 from working mf4 metadata + version number
mdfWrite("NewMF4.mf4",TTdata); % Writes timetable data to new mf4 file
  댓글 수: 2
Vinayak
Vinayak 2024년 1월 18일
Hi James,
Could you please share a sample data. When I tried with a random data and created an MDF file. On using mdfRead, I get time as the first position automatically.
James
James 2024년 1월 18일
Hi Vinayak,
Looking at the image you have shared, I also get the same thing. However, this does not mean that the Time is actually in the first position. This is just how Matlab displays it's timetables I believe.
The time variable I am refering to is one created in the process of using mdfWrite as my initial data does not have this time column.

댓글을 달려면 로그인하십시오.

답변 (1개)

Shubh
Shubh 2024년 1월 19일
Hi James,
In MATLAB, when writing data to an MF4 (Measurement Data Format version 4) file, the default behavior is to append the time channel as the last channel in the file. However, for your specific requirement where the time channel needs to be the first channel, you can rearrange the timetable TTdata before writing it to the MF4 file.
You can ensure that the time channel (usually the first column in a timetable) is positioned at the beginning of the data set. Here's the modified version of your code to achieve this:
clear
clc
% Frequency and read data
frequency = 20; % Hz
TTdata = readtimetable("ExcelData.xlsx", "SampleRate", frequency);
% Ensure the time channel is the first column
% Assuming 'time' is the name of your time column
% If not, replace 'time' with the actual name of the time column
TTdata = [TTdata(:, 'time'), TTdata(:, setdiff(TTdata.Properties.VariableNames, 'time'))];
% Read and setup MF4 file information
info = mdfInfo("WorkingMF4.mf4");
info.Version = "4.10";
% Create a new MF4 file
mdfCreate("NewMF4.mf4", "FileInfo", info);
% Write the timetable data to the new MF4 file
mdfWrite("NewMF4.mf4", TTdata);
This code snippet first reads the timetable from the Excel file. Then, it rearranges the timetable so that the time channel is the first column. After that, it reads the metadata from an existing MF4 file, sets the version, creates a new MF4 file, and finally writes the rearranged timetable data to this new MF4 file.
Make sure to replace 'time' with the actual name of your time column if it's different. The 'setdiff' function is used to ensure that the time column is not duplicated in the timetable.
Hope this helps!
  댓글 수: 2
James
James 2024년 1월 22일
Thank you for your suggestion, however, this does not achieve the result that I am intending.
My Excel file does not contain a 'time' column to start with, hence I would not be able to move that column to the front. However, when manipulating the file to include a 'time' column, the column is automatically renamed and the secondary time column is still created and set as the master channel.
My ideal goal is to only have a singular time channel in the first position which is set as being the master.
Hope this makes sense.
Shubh
Shubh 2024년 1월 22일
Hi James,
In your case, since the Excel file does not contain an explicit 'time' column, and you want to create a singular time channel set as the master, we need to handle this differently. When you read data from an Excel file into a timetable in MATLAB, it automatically creates a time variable based on the row times.
We can manipulate the timetable to use this automatically created time variable as the master time channel. Here's an approach to achieve this:
  1. Read data from the Excel file into a timetable.
  2. Convert the row times of the timetable to a new variable (column) in the timetable.
  3. Write this modified timetable to the MF4 file.
Here's how you can implement this:
clear
clc
% Frequency and read data
frequency = 20; % Hz
TTdata = readtimetable("ExcelData.xlsx", "SampleRate", frequency);
% Convert row times to a new variable in the timetable
TTdata.Time = TTdata.Properties.RowTimes;
% Move the 'Time' column to the first position
TTdata = movevars(TTdata, 'Time', 'Before', TTdata.Properties.VariableNames{1});
% Read and setup MF4 file information
info = mdfInfo("WorkingMF4.mf4");
info.Version = "4.10";
% Create a new MF4 file
mdfCreate("NewMF4.mf4", "FileInfo", info);
% Write the timetable data to the new MF4 file
mdfWrite("NewMF4.mf4", TTdata);
In this code:
  • The 'readtimetable' function reads the Excel file and converts it into a timetable. MATLAB automatically assigns row times based on the sampling rate.
  • The 'Time' column is added to the timetable, containing the row times.
  • The 'movevars' function is used to move the Time column to the first position.
  • The rest of the code is for creating and writing to the MF4 file.
This should result in an MF4 file where the first channel is the singular time channel set as the master.
Let me know if this helps!

댓글을 달려면 로그인하십시오.

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by