Code covered by the BSD License  

Highlights from
edfRead

4.83333

4.8 | 7 ratings Rate this file 130 Downloads (last 30 days) File Size: 3.44 KB File ID: #31900
image thumbnail

edfRead

by Brett Shoelson

 

21 Jun 2011 (Updated 23 Jan 2012)

A simple file reader for European Data Formatted (EDF-) files.

| Watch this File

File Information
Description

Read European Data Format file into MATLAB
 
[hdr, record] = edfRead(fname)
Reads data from ALL RECORDS of file fname ('*.edf'). Header information is returned in structure hdr, and the signals (waveforms) are returned in structure record, with waveforms associated with the records returned as fields titled 'data' of structure record.
 
[...] = edfRead(fname, 'assignToVariables', assignToVariables)
Triggers writing of individual output variables, as defined by field 'labels', into the caller workspace.
 
FORMAT SPEC: Source: http://www.edfplus.info/specs/edf.html

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File  
Everyone's Tags
edf european data format ecg ekg(6)
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
01 May 2013 Brett Shoelson

Farid, I thought that's what I did. Perhaps if my response is unclear, or if I misunderstood your question, you can email me with a better explanation of what you would like to do with EDFREAD.
Cheers,
Brett
char(cumsum([98 16 -13 15 0 -70 69 -11 7 -10 7 7 -4 -1 -46 45 -12 19 -12 15 -8 3 -7 8 -69 53 12 -2]))

01 May 2013 Farid

Thanks Brett for your quick and clear response. Could you please suggest a way to read segments of data (e.g specific signals in shorter segments).
Thanks

Farid

01 May 2013 Brett Shoelson

Farid,
Number of channels (signals?) is specified in line 124:
hdr.ns = str2double(fread(fid,4,'*char')');

In subsequent lines (125,128,132,...) the files reads 1:hdr.ns. If you wanted to, you could modify those calls to read (for example) signals [1,3,5,6,7] by changing instances of

for ii = 1:hdr

to

for ii = [1,3,5:7]

HTH,
Brett

01 May 2013 Farid

Thanks for useful code. I wonder to know how I can use the edfread to read specific channels? I have huge file of several channels and am interested on some of them. The comments on MATLAB code is not clear to me.
Thanks

29 Apr 2013 Brett Shoelson

@Oleksandr (and Ibraheem):
Thanks for the reviews/comments. My goal in writing this was to facilitate using MATLAB to analyze data stored in EDF format.

I encourage you to share your EDFWrite function, if you write one; creating that capability is a bit outside of my job description. ;)

I'm curious, though: what is your rationale for exporting to EDF after you've brought data in to MATLAB for analysis? Is there something you need to do with them that you can't do with MathWorks' tools? (Or did you just want to share with non-MATLAB users?)

Brett (the MATLAB guy)

29 Apr 2013 Oleksandr Makeyev

Hi Brett,
This is a great function, thank you very much for making and sharing it!
Sorry to repeat a question that already has been asked before but is there an edfWrite (or edfSave) by any chance? I'd love to save myself some time making one if at all possible.
Once again, thank you for your work!
Regards,

28 Apr 2013 Hisham

Thanks Brett. I discovered the problem. For some reason when I was downloading the file into my Matlab directory The file got corrupted in some way that its much different than the original page. I just copied the text Matlab function into a blank m file in my Matlab directory and so I was able to run the function without errors. Thanks

26 Apr 2013 Brett Shoelson

Hisham, Would you either try to download the function again and re-try to read the file, or send me the file you downloaded. I'd like to understand what's going on.
Thanks,
Brett
char(cumsum([98 16 -13 15 0 -70 69 -11 7 -10 7 7 -4 -1 -46 45 -12 19 -12 15 -8 3 -7 8 -69 53 12 -2]))

25 Apr 2013 Hisham

Thanks Brett for your quick Reply. I am using Matlab R2012a and I am not sure where is the problem. I tried the r01.edf you recommended but the same thing happended. when I run the file I got the message 'Matlab unexpected operator' on every line when I comment out everything before it. I am not doing an coding else than invoking the file by edfread('r01.edf') is this right? Also I don't think line 13 is a comment (although it seems not doing anything in the code). I have downloaded the file edfread directly. So is there anything I am missing? Thanks alot for your help.

25 Apr 2013 Brett Shoelson

Hisham, line 13 is already a comment, isn't it? Can you tell me what platform you're using, and what version of MATLAB? And can you download a signal from Physionet and try to read it? (For instance:

http://www.physionet.org/physiobank/database/adfecgdb/r01.edf

).

If you can read that signal, it would suggest there's something in your data file that might be problematic.

Thanks,
Brett

25 Apr 2013 Hisham

I am trying to use this function to read an edf file but I am getting the following error always
Error: File: edfread.m Line: 13 Column: 1
Unexpected MATLAB operator.
Even if I comment out Line: 13 the same error occurs on following lines. Is there anything I am missing to run the edf code.

24 Jan 2013 Ibraheem Al-Dhamari

Thanks for sharing. It would be great if you make it two directions by adding export feature (write matlab variables to edf).
Best regards!

28 Sep 2012 Behnaz  
28 Sep 2012 Behnaz  
29 Feb 2012 Joseph Isler

Excellent, and timing is perfect I just needed it now!

28 Jan 2012 Andrey Shapkin

Great piece of work, thanks.

function [data,header] = edfread(filename)
% Read European Data Format file into MATLAB

fid = fopen(filename,'r','ieee-le');
% PART1
hdr = char(fread(fid,256,'uchar')');

header.ver=str2num(hdr(1:8)); % 8 ascii : version of this data format (0)
header.patientID = char(hdr(9:88)); % 80 ascii : local patient identification
header.recordID = char(hdr(89:168)); % 80 ascii : local recording identification
header.startdate=char(hdr(169:176)); % 8 ascii : startdate of recording (dd.mm.yy)
header.starttime = char(hdr(177:184)); % 8 ascii : starttime of recording (hh.mm.ss)
header.length = str2num (hdr(185:192)); % 8 ascii : number of bytes in header record
reserved = hdr(193:236); % [EDF+C ] % 44 ascii : reserved
header.records = str2num (hdr(237:244)); % 8 ascii : number of data records (-1 if unknown)
header.duration = str2num (hdr(245:252)); % 8 ascii : duration of a data record, in seconds
header.channels = str2num (hdr(253:256));% 4 ascii : number of signals (ns) in data record

%%%% PART2

header.label=cellstr(char(fread(fid,[16,header.channels],'char')')); % ns * 16 ascii : ns * label (e.g. EEG FpzCz or Body temp)
header.transducer =cellstr(char(fread(fid,[80,header.channels],'char')')); % ns * 80 ascii : ns * transducer type (e.g. AgAgCl electrode)
header.units = cellstr(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * physical dimension (e.g. uV or degreeC)
header.physmin = str2num(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * physical minimum (e.g. -500 or 34)
header.physmax = str2num(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * physical maximum (e.g. 500 or 40)
header.digimin = str2num(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * digital minimum (e.g. -2048)
header.digimax = str2num(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * digital maximum (e.g. 2047)
header.prefilt =cellstr(char(fread(fid,[80,header.channels],'char')')); % ns * 80 ascii : ns * prefiltering (e.g. HP:0.1Hz LP:75Hz)
header.samplerate = str2num(char(fread(fid,[8,header.channels],'char')')); % ns * 8 ascii : ns * nr of samples in each data record
reserved = char(fread(fid,[32,header.channels],'char')'); % ns * 32 ascii : ns * reserved

% DATA read

Ch_data = fread(fid,'int16');

if header.records<0, % number of data records (-1 if unknown)
R=sum(header.duration*header.samplerate);
header.records=fix(length(Ch_data)./R);
end

% reshape data
Ch_data=reshape(Ch_data, [], header.records);


% scale set
sf = (header.physmax - header.physmin)./(header.digimax - header.digimin);
dc = header.physmax - sf.* header.digimax;

data=cell(1, header.channels);
Rs=cumsum([1; header.duration.*header.samplerate]); % index of block data -> Rs(k):Rs(k+1)-1

for k=1:header.channels
data{k}=reshape(Ch_data(Rs(k):Rs(k+1)-1, :), [], 1);
% scale data
data{k}=data{k}.*sf(k)+dc(k);
end

% data=cell2mat(data);

Updates
23 Jan 2012

Added copyright.

Contact us