Write the Timetable data to the EDF file

10 views (last 30 days)
>> 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 Comments
Shashi Kiran
Shashi Kiran on 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 on 2 Sep 2024
Edited: Walter Roberson on 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)

Sign in to comment.

Accepted Answer

Umar
Umar on 2 Sep 2024

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 Comments
Umar
Umar on 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 on 3 Sep 2024
I think it's very hlepful ,thank you very much!

Sign in to comment.

More Answers (1)

Shashi Kiran
Shashi Kiran on 2 Sep 2024
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 Comments
Shashi Kiran
Shashi Kiran on 2 Sep 2024
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 on 3 Sep 2024
Edited: Wensor on 3 Sep 2024
I think it's very helpful,Thank you so much!

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!