Time channel position incorrect when writing timetable to MF4
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Vinayak
il 18 Gen 2024
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.
Risposte (1)
Shubh
il 19 Gen 2024
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 Commenti
Shubh
il 22 Gen 2024
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:
- Read data from the Excel file into a timetable.
- Convert the row times of the timetable to a new variable (column) in the timetable.
- 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!
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!