2 views (last 30 days)
Show older comments
James on 16 Jan 2024
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/2070546-time-channel-position-incorrect-when-writing-timetable-to-mf4
Commented: Shubh on 22 Jan 2024
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
Vinayak on 18 Jan 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2070546-time-channel-position-incorrect-when-writing-timetable-to-mf4#comment_3033136
⋮
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 on 18 Jan 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2070546-time-channel-position-incorrect-when-writing-timetable-to-mf4#comment_3033146
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.
Sign in to comment.
Sign in to answer this question.
Answers (1)
Shubh on 19 Jan 2024
Open in MATLAB Online
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 Comments Show NoneHide None
Show NoneHide None
James on 22 Jan 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2070546-time-channel-position-incorrect-when-writing-timetable-to-mf4#comment_3036981
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 on 22 Jan 2024
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2070546-time-channel-position-incorrect-when-writing-timetable-to-mf4#comment_3037056
Open in MATLAB Online
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!
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABData Import and AnalysisData Import and ExportStandard File FormatsSpreadsheets
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office