Write the Timetable data to the EDF file

>> hdr = edfheader("EDF");
>> hdr.NumSignals = 3;
hdr.NumDataRecords = 3;
>> edfw = edfwrite("rand.edf",hdr,TT);
Incorrect use of edfwrite
annotations should be arrays with columns equal to 2.
But I haven't annotations
How can I write a Timetable data into a EDF File?

2 commentaires

Shashi Kiran
Shashi Kiran le 2 Sep 2024
I used edfwrite on my data without annotations, and it worked fine. Could you share your Timetable data(TT) so I can look into it further?
Wensor
Wensor le 2 Sep 2024
Modifié(e) : Walter Roberson le 3 Sep 2024
Thank you @Shashi Kiran
Actually, I'm doing a practice.My data is
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed)

Connectez-vous pour commenter.

 Réponse acceptée

Umar
Umar le 2 Sep 2024

0 votes

Hi @Wensor ,

You asked, “How can I write a Timetable data into a EDF File?”

To address your inquiry regarding writing a Timetable to an EDF file in MATLAB, I broke down your requirements and analyzed the provided code.

Data Structure: You are working with a Timetable (TT) containing signal data. EDF File Format: You are attempting to write this data to an EDF file using the edfwrite function. Error Handling: The error indicates that annotations are expected as a two-column array, but you do not have any annotations to include.

So, to write your Timetable data into an EDF file without annotations, you need to make that the data structure and the header properties are correctly set up based on mathworks documentation provided in the link below.

https://www.mathworks.com/help/signal/ref/edfwrite.html

Below is the complete and corrected code based on your original snippet.

% Create your Timetable data
time = duration(0, 0, 0.1*(0:6)');  % Create a duration vector for time
data1 = randn(length(time), 1);  % Signal 1
data2 = randn(length(time), 1);  % Signal 2
data3 = randn(length(time), 1);  % Signal 3
% Create a timetable
TT = timetable(time, data1, data2, data3);
% Prepare the EDF Header
hdr = edfheader("EDF");
hdr.NumSignals = 3;  % Number of signals
hdr.NumDataRecords = height(TT);  % Number of data records
hdr.DataRecordDuration = duration(0, 0, 0.1); % Duration of each data record
% Specify signal labels and other properties
hdr.SignalLabels = ["Signal 1"; "Signal 2"; "Signal 3"];
hdr.TransducerTypes = ["EEG"; "EEG"; "EEG"];  % Example transducer type
hdr.PhysicalDimensions = ["uV"; "uV"; "uV"];  % Signal units
hdr.PhysicalMin = [-500; -500; -500];  % Min physical values
hdr.PhysicalMax = [500; 500; 500];  % Max physical values
hdr.DigitalMin = [-32768; -32768; -32768];  % Min digital values
hdr.DigitalMax = [32767; 32767; 32767];  % Max digital values
% Prepare signal data
sigdata = [TT.data1, TT.data2, TT.data3];  % Extract signal data from the 
timetable
% Write to EDF file without annotations
edfw = edfwrite("rand.edf", hdr, sigdata);  % Correct method call

Please see attached.

If you later decide to include annotations, you would need to create a separate timetable for them, ensuring it contains the required columns: Onset, Annotations, and Duration. Make sure that the length of the sigdata matches the number of data records in the header.

Hope this answers your questions please let me know if you have any further questions.

4 commentaires

Wensor
Wensor le 2 Sep 2024
Modifié(e) : Wensor le 2 Sep 2024
Thank you@Umar
Can I understand that the Timetable cannot be directly written into EDF, but data needs to be extracted?
Then I need to extract the Time column, but there are errors
>> sigdata = [TT.Time,TT.Temp,TT.Pressure,TT.WindSpeed]
错误使用 datetime/horzcat
所有输入都必须为日期时间,或者为日期/时间字符向量或日期/时间字符串。
That means
datetime/horzcat is incorrectly used
All inputs must be date-time, either a date/time character vector or a date/time string.
My data is
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed)
Umar
Umar le 2 Sep 2024

Hi @Wensor,

Thanks for sharing this code, now it makes it easier to achieve your task. So,what you need to do is to extract the relevant data from the Timetable into a suitable format. Here's a step-by-step approach to correctly achieve this

Extract Signal Data: You will extract the measurement variables from your Timetable and convert the time information into a numeric representation (e.g., seconds since the start).

Prepare the EDF Header: Using edfheader, you will set up the EDF file structure, specifying the number of signals and other required metadata.

Write to EDF File: Finally, you will call edfwrite with the prepared header and signal data.

Here's a complete and corrected version of your MATLAB code:

% Create your Timetable data
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18   
10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3; 39.1; 42.3];
Pressure = [30.1; 30.03; 29.9];
WindSpeed = [13.4; 6.5; 7.3];
TT = timetable(MeasurementTime, Temp, Pressure, WindSpeed);
% Convert MeasurementTime to numeric value (seconds since start)
startTime = TT.MeasurementTime(1);
timeInSeconds = seconds(TT.MeasurementTime - startTime);
% Prepare the EDF Header
hdr = edfheader("EDF");
hdr.NumSignals = 3;  % Number of signals
hdr.NumDataRecords = height(TT);  % Number of data records
hdr.DataRecordDuration = seconds(1); % Example: 1 second duration
% Specify signal labels and other properties
hdr.SignalLabels = ["Temperature"; "Pressure"; "Wind Speed"];
hdr.TransducerTypes = ["Temp Sensor"; "Pressure Sensor"; 
"Anemometer"];
hdr.PhysicalDimensions = ["°C"; "hPa"; "m/s"];
hdr.PhysicalMin = [0; 0; 0];  
% Adjust these based on your data   range
hdr.PhysicalMax = [50; 110; 20];  % Adjust these based on your  
data range
hdr.DigitalMin = [-32768; -32768; -32768];  % Min digital values
hdr.DigitalMax = [32767; 32767; 32767];  % Max digital values
% Prepare signal data
sigdata = [TT.Temp, TT.Pressure, TT.WindSpeed];  % Extract     signals from the timetable
% Write to EDF file without annotations
edfw = edfwrite("measurements.edf", hdr, sigdata);  % Correct 
method call

Please let me know if this helped resolve your problem.

Umar
Umar le 3 Sep 2024

Hi @Wensor,

I have executed the code above and attached the file below. Please let me know if this answers your question.

Wensor
Wensor le 3 Sep 2024
I think it's very hlepful ,thank you very much!

Connectez-vous pour commenter.

Plus de réponses (1)

I see you are using edfwrite on timetables. The edfwrite function is specifically designed to work with numerical data, not with timetables. However, you can convert the timetable to a numeric matrix and then use edfwrite to write the data to an EDF file.
Below is an implementation based on the data you have provided:
% Create timetable with your data
MeasurementTime = datetime({'2015-12-18 08:03:05'; '2015-12-18 10:03:17'; '2015-12-18 12:03:13'});
Temp = [37.3; 39.1; 42.3];
Pressure = [30.1; 30.03; 29.9];
WindSpeed = [13.4; 6.5; 7.3];
TT = timetable(MeasurementTime, Temp, Pressure, WindSpeed);
% Prepare signal data
sigdata = [TT.Temp, TT.Pressure, TT.WindSpeed];
% Prepare the EDF Header
hdr = edfheader("EDF");
hdr.NumSignals = 3;
hdr.NumDataRecords = height(TT);
hdr.DataRecordDuration = duration(0, 0, 0.1);
% Specify signal labels and other properties
hdr.SignalLabels = ["Temperature"; "Pressure"; "Wind Speed"];
hdr.PhysicalDimensions = ["C"; "hPa"; "m/s"];
hdr.PhysicalMin = [min(Temp); min(Pressure); min(WindSpeed)];
hdr.PhysicalMax = [max(Temp); max(Pressure); max(WindSpeed)];
hdr.DigitalMin = [-32768; -32768; -32768];
hdr.DigitalMax = [32767; 32767; 32767];
% Write the data to an EDF file
edfw = edfwrite("rand.edf", hdr, sigdata);
disp(edfw)
edfwrite with: File properties (Read-only): Filename: "rand.edf" FileType: "EDF" FileModDate: "02-Sep-2024 06:38:10" FileSize: 1042 Show all properties
Ensure that you have correctly defined the EDF header according to your requirements.
Refer the below documentation for more information regarding edfwrite
  1. https://in.mathworks.com/help/signal/ref/edfwrite.html#mw_2b309c94-5d2e-455e-86ef-d8dc48275b0b
Hope this helps!

3 commentaires

Wensor
Wensor le 2 Sep 2024
Thank you @Shashi Kiran
I also need the Time column, but when I extract them, there are errors
>> sigdata = [TT.Time,TT.Temp,TT.Pressure,TT.WindSpeed]
Then Ihave the error
datetime/horzcat is incorrectly used
All inputs must be date-time, either a date/time character vector or a date/time string.
Since we cannot use datetime directly in the EDF file, we can use relative time to store the time data in hours or minutes as required.
Here is the revised code as per your requirement.
% Create timetable with your data
MeasurementTime = datetime({'2015-12-18 08:03:05'; '2015-12-18 10:03:17'; '2015-12-18 12:03:13'});
Temp = [37.3; 39.1; 42.3];
Pressure = [30.1; 30.03; 29.9];
WindSpeed = [13.4; 6.5; 7.3];
TT = timetable(MeasurementTime, Temp, Pressure, WindSpeed);
timeHours = hours(TT.MeasurementTime - TT.MeasurementTime(1));
sigdata = [timeHours, TT.Temp, TT.Pressure, TT.WindSpeed];
% Prepare the EDF Header
hdr = edfheader("EDF");
hdr.NumSignals = 4;
hdr.NumDataRecords = height(TT);
hdr.DataRecordDuration = duration(0, 0, 0.1);
% Specify signal labels and other properties
hdr.SignalLabels = ["Time", "Temperature", "Pressure", "Wind Speed"];
hdr.PhysicalDimensions = ["h", "C", "hPa", "m/s"]; % Dimensions for each signal
hdr.PhysicalMin = [min(timeHours), min(Temp), min(Pressure), min(WindSpeed)];
hdr.PhysicalMax = [max(timeHours), max(Temp), max(Pressure), max(WindSpeed)];
hdr.DigitalMin = [-32768, -32768, -32768, -32768];
hdr.DigitalMax = [32767, 32767, 32767, 32767];
% Write the data to an EDF file
edfw = edfwrite("rand.edf", hdr, sigdata);
disp(edfw)
edfwrite with: File properties (Read-only): Filename: "rand.edf" FileType: "EDF" FileModDate: "02-Sep-2024 08:01:58" FileSize: 1304 Show all properties
Wensor
Wensor le 3 Sep 2024
Modifié(e) : Wensor le 3 Sep 2024
I think it's very helpful,Thank you so much!

Connectez-vous pour commenter.

Catégories

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by