Main Content

edfwrite

Create or modify EDF or EDF+ file

Since R2021a

    Description

    Create an edfwrite object to write or modify a European Data Format (EDF) or EDF+ file.

    Creation

    Description

    edfw = edfwrite(filename) creates an edfwrite object for an existing EDF or EDF+ file specified by filename.

    edfw = edfwrite(filename,hdr,sigdata) creates an edfwrite object and a new EDF or EDF+ file with signal data, sigdata. File properties are specified in the header structure, hdr.

    edfw = edfwrite(filename,hdr,annotationslist) creates an edfwrite object and a new EDF or EDF+ file with annotations, annotationslist.

    edfw = edfwrite(filename,hdr,sigdata,annotationslist) creates an edfwrite object and a new EDF or EDF+ file with signal data and annotations.

    example

    edfw = edfwrite(___,Name,Value) sets Properties using name-value arguments. You can specify DataRecordTimes, AnnotationsEncoding, and InputSampleType.

    Input Arguments

    expand all

    Name of EDF or EDF+ file, specified as a character vector or string scalar.

    Depending on the location of the file, filename can take one of these forms.

    Location

    Form

    Current folder or folder on the MATLAB® path

    Specify the name of the file in filename.

    Example: 'data.edf'

    File in a folder

    If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name.

    Example: 'C:\myFolder\data.edf'

    Example: 'myDir\myFile.ext'

    Note

    edfwrite does not support EyeLink® EDF files.

    Data Types: char | string

    Header details, specified as a structure. See edfheader for more information.

    Data Types: struct

    Signal data, specified as a numeric matrix with one or more columns or a cell array of numeric vectors.

    Data Types: double | cell

    Annotations, specified as a timetable containing these variables:

    • Onset — Time at which the annotation occurred, expressed as a duration indicating the number of seconds elapsed since the start time of the file. Use Onset to specify the rowTimes in the timetable.

    • Annotations — A string that contains the annotation text.

    • Duration — A duration scalar that indicates the duration of the event described by the annotation. If the file does not specify an annotation duration, this variable is returned as NaN.

    Data Types: table

    Properties

    expand all

    File Properties

    This property is read-only.

    File name, returned as a string scalar.

    Example: "ecg_20200411_120.edf"

    Data Types: string

    File type, returned as "EDF" or "EDF+".

    Data Types: string

    This property is read-only.

    Date last modified, returned as a string scalar with the date and time the file was last modified.

    Example: "11-Apr-2020 15:38:37"

    This property is read-only.

    File size in bytes, returned as an integer scalar.

    Example: 4040992

    Data Types: double

    Header Properties

    This property is read-only.

    Data format version, returned as "0".

    Data Types: string

    This property is read-only.

    Patient identification details, returned as a string scalar. Patient identification details can include Patient ID, sex or gender, birth date in 'dd-MMM-yyyy' format, and name.

    Example: "X F X 120 04-JUL-1982"

    Data Types: string

    This property is read-only.

    Recording identification details, returned as a string scalar. Recording identification details may include its start date and time, the ID of the technician that made the recording, and the ID of the equipment that made the recording.

    Example: "Startdate 04-JUL-1982 X X X"

    Data Types: string

    This property is read-only.

    Recording start date, returned as a string scalar in 'dd.MM.yy' format.

    Example: "04.07.82"

    Data Types: string

    This property is read-only.

    Recording start time, returned as a string scalar in 'HH.mm.ss' format.

    Example: "17.16.37"

    Data Types: string

    This property is read-only.

    Header size in bytes, returned as an integer scalar. HeaderBytes is given by (256 + NumSignals × 256) bytes. The first 256 bytes correspond to a static header and are required for all EDF and EDF+ files. The other bytes depend on the number of signals present in the data records.

    Example: 2048

    Data Types: double

    This property is read-only.

    EDF+ interruption information, returned as "EDF+C" or "EDF+D" for EDF+ compliant files.

    • "EDF+C" — The recording is continuous: There are no interruptions and all data records are contiguous, such that the start time of each data record coincides with the start time of the previous record plus its duration.

    • "EDF+D" — The recording is discontinuous with interruptions between consecutive data records.

    For files that are not EDF+ compliant, this property is an empty string ("").

    Data Types: string

    This property is read-only.

    Number of data records in file, returned as an integer scalar.

    Note

    If filename is not EDF compliant, NumDataRecords can be set to -1 when the number of data records is unknown. If filename is EDF compliant, NumDataRecords must be set to a positive integer. If filename has Reserved set to a nonempty string and NumDataRecords set to -1, edfinfo throws an error.

    Data Types: double

    This property is read-only.

    Duration of each data record, returned as a duration scalar.

    Data Types: duration

    This property is read-only.

    Number of signals in file, returned as an integer scalar.

    Data Types: double

    Start time of each data record, returned as a duration vector. DataRecordTimes must be specified for an EDF+ file with discontinuous record start times. The vector must be equal in length to NumDataRecords.

    Data Types: duration

    Signal Properties

    This property is read-only.

    Signal names, returned as a string vector of length NumSignals.

    ["Thorax 1";"Abdomen 3"]

    Data Types: string

    This property is read-only.

    Transducer details, returned as a string vector of length NumSignals. Each element of TransducerTypes contains details about the transducer used to obtain the corresponding signal in SignalLabels.

    Example: ["AgAgCl electrodes";"thermistor"]

    Data Types: string

    This property is read-only.

    Signal data units, returned as a string vector of length NumSignals. Each element of PhysicalDimensions contains the measurement units used to express the values of the corresponding signal in SignalLabels.

    Example: ["uV";"mV"]

    Data Types: string

    This property is read-only.

    Signal minimum physical value, returned as a numeric vector of length NumSignals. Each element of PhysicalMin contains the minimum physical value of the corresponding signal in SignalLabels.

    Data Types: double

    This property is read-only.

    Signal maximum physical value, returned as a numeric vector of length NumSignals. Each element of PhysicalMax contains the maximum physical value of the corresponding signal in SignalLabels.

    Data Types: double

    This property is read-only.

    Signal minimum digital value, returned as a numeric vector of length NumSignals. Each element of DigitalMin contains the minimum digital value of the corresponding signal in SignalLabels.

    Data Types: double

    This property is read-only.

    Signal maximum digital value, returned as a numeric vector of length NumSignals. Each element of DigitalMax contains the maximum digital value of the corresponding signal in SignalLabels.

    Data Types: double

    This property is read-only.

    Signal data units, returned as a string vector of length NumSignals. Each element of Prefilter contains details about the filters, if any, used to preprocess the corresponding signal in SignalLabels.

    Example: ["HP:10Hz LP:80Hz N:60Hz";"HP:0.1Hz LP:90Hz N:60Hz"]

    Data Types: string

    This property is read-only.

    Number of samples in signal, returned as a numeric vector of length NumSignals. Each element of NumSamples contains the number of samples in the corresponding signal in SignalLabels.

    Data Types: double

    This property is read-only.

    Additional signal information, returned as a string vector of length NumSignals. Each element of SignalReserved contains additional information, if any, about the corresponding signal in SignalLabels.

    Data Types: string

    Input sample type of signal data, returned as "digital" or "physical". The function defaults to "digital" and writes the signal data into the file with no digital scaling. If 'InputSampleType' is set to "physical", then edfwrite applies digital scaling to the signal data.

    Data Types: string

    Annotation Properties

    This property is read-only.

    Annotations present in signal records, returned as a timetable containing these variables:

    • Onset — Time at which the annotation occurred, expressed as a duration indicating the number of seconds elapsed since the start time of the file.

    • Annotations — A string that contains the annotation text.

    • Duration — A duration scalar that indicates the duration of the event described by the annotation. If the file does not specify an annotation duration, this variable is returned as NaN.

    Data Types: table

    Encoding format used to write annotations into the file, returned as "US-ASCII", "UTF-8", or "LATIN1".

    Data Types: string

    Object Functions

    addAnnotationsAdd annotations to EDF or EDF+ file
    addSignalsAdd new signals to EDF or EDF+ file
    deleteAnnotationsDelete annotations from EDF or EDF+ file
    deleteSignalsDelete signals from EDF or EDF+ file
    modifyAnnotationsModify annotations in EDF or EDF+ file
    modifyHeaderModify header details of EDF or EDF+ file
    modifySignalsModify signals in EDF or EDF+ file

    Examples

    collapse all

    Load EMGdata.mat into the workspace. The file contains eight channels of surface electromyography (EMG) data [1] recorded from eight arm muscles. The data is available at www.sce.carleton.ca/faculty/chan/index.php?page=matlab. The sample rate is 1000 Hz. Plot the signals.

    load EMGdata
    
    fs = 1000;
    t = 0:1/fs:(size(data,1)-1)/fs;
    stackedplot(t,data)

    The bursts of increased signal amplitude correspond to different forearm motions that last 3 seconds each. EMGindex.mat contains the type of motion and the start index (sample) of each motion in two variables: motion and start_index. The motion types are:

    1. Hand open

    2. Hand close

    3. Wrist flexion

    4. Wrist extension

    5. Supination

    6. Pronation

    7. Rest

    Load the data into the workspace.

    load EMGindex

    Create a timetable of annotations.

    1. Use Onset to specify the row times. Onset contains the start index of each motion in seconds.

    2. Annotations specifies the types of motion as a string array.

    3. Duration specifies the duration of each motion in seconds.

    Onset = seconds(start_index./fs);
    Annotations = string(motion);
    Duration = seconds(ones(length(Onset),1)*3);
    
    annotationslist = timetable(Onset,Annotations,Duration);

    Use edfheader to create a header structure for the EDF+ file and set the properties. See edfheader for more information.

    hdr = edfheader("EDF+");
    hdr.NumDataRecords = 1;
    hdr.DataRecordDuration = seconds(length(data(:,1))/fs);
    hdr.NumSignals = 8;
    hdr.SignalLabels = ["F1" "F2" "F3" "F4" "F5" "F6" "F7" "B1"];
    hdr.PhysicalDimensions = repelem("mV",8);
    hdr.PhysicalMin = min(data);
    hdr.PhysicalMax = max(data);
    hdr.DigitalMin = [-32768 -32768 -32768 -32768 -32768 -32768 -32768 -32768];
    hdr.DigitalMax = [32767 32767 32767 32767 32767 32767 32767 32767];

    Write an EDF+ file containing the header structure, signal data, and annotations. Specify the input sample type as physical. The file is saved in the current working directory.

    edfw = edfwrite("armEMG.edf",hdr,data,annotationslist,'InputSampleType',"physical");

    Display information about the file.

    edfinfo("armEMG.edf")
    ans = 
      edfinfo with properties:
    
                  Filename: "armEMG.edf"
               FileModDate: "12-Feb-2024 21:43:34"
                  FileSize: 4803836
                   Version: "0"
                   Patient: "1234567 F 12-FEB-2024 Patient_1"
                 Recording: "Startdate 12-FEB-2024 MW_1234567 MW_Inv_01 MW_Eq_01"
                 StartDate: "12.02.24"
                 StartTime: "21.43.33"
               HeaderBytes: 2560
                  Reserved: "EDF+C"
            NumDataRecords: 1
        DataRecordDuration: 300.03 sec
                NumSignals: 8
              SignalLabels: [8x1 string]
           TransducerTypes: [8x1 string]
        PhysicalDimensions: [8x1 string]
               PhysicalMin: [8x1 double]
               PhysicalMax: [8x1 double]
                DigitalMin: [8x1 double]
                DigitalMax: [8x1 double]
                 Prefilter: [8x1 string]
                NumSamples: [8x1 double]
            SignalReserved: [8x1 string]
               Annotations: [28x2 timetable]
    
    

    You can use EDF File Analyzer to view the signals and annotations stored in the file. Use the Signal Separation option to separate the signals for better visualization.

    edfApp_EMG.png

    Delete the EDF+ file. Comment out this code if you want to keep the file.

    delete armEMG.edf

    Tips

    • To create an EDF+ file containing only annotations, specify NumDataRecords and NumSignals as 0, DataRecordDuration as a duration scalar with value 0, and all signal properties as empty.

    • Launch the EDF File Analyzer app to visualize the signals in your EDF or EDF+ file.

    References

    [1] Chan, Adrian D.C., and Geoffrey C. Green. 2007. "Myoelectric Control Development Toolbox". Paper presented at 30th Conference of the Canadian Medical & Biological Engineering Society, Toronto, Canada, 2007.

    [2] Kemp, Bob, Alpo Värri, Agostinho C. Rosa, Kim D. Nielsen, and John Gade. “A Simple Format for Exchange of Digitized Polygraphic Recordings.” Electroencephalography and Clinical Neurophysiology 82, no. 5 (May 1992): 391–93. https://doi.org/10.1016/0013-4694(92)90009-7.

    [3] Kemp, Bob, and Jesus Olivan. "European Data Format 'plus' (EDF+), an EDF Alike Standard Format for the Exchange of Physiological Data." Clinical Neurophysiology 114, no. 9 (2003): 1755–1761. https://doi.org/10.1016/S1388-2457(03)00123-8.

    Version History

    Introduced in R2021a

    See Also

    Apps

    Objects

    Functions

    External Websites