-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_ctf_dat.m
66 lines (57 loc) · 1.62 KB
/
read_ctf_dat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function [meg] = read_ctf_dat(filename);
% READ_CTF_DAT reads MEG data from an ascii format CTF file
%
% meg = read_ctf_dat(filename)
%
% returns a structure with the following fields:
% meg.data Nchans x Ntime
% meg.time 1xNtime in miliseconds
% meg.trigger 1xNtime with trigger values
% meg.label 1xNchans cell array with channel labels (string)
% Copyright (C) 2002, Robert Oostenveld
%
fid = fopen(filename, 'r');
if fid==-1
error(sprintf('could not open file %s', filename));
end
% read the sample number
line = fgetl(fid);
[tok, rem] = strtok(line, ':');
meg.sample = str2num(rem(2:end));
% read the time of each sample and convert to miliseconds
line = fgetl(fid);
[tok, rem] = strtok(line, ':');
meg.time = 1000*str2num(rem(2:end));
% read the trigger channel
line = fgetl(fid);
[tok, rem] = strtok(line, ':');
meg.trigger = str2num(rem(2:end));
% read the rest of the data
meg.data = [];
meg.label = {};
chan = 0;
while (1)
line = fgetl(fid);
if ~isempty(line) & line==-1
% reached end of file
break
end
[tok, rem] = strtok(line, ':');
if ~isempty(rem)
chan = chan + 1;
meg.data(chan, :) = str2num(rem(2:end));
meg.label{chan} = fliplr(deblank(fliplr(deblank(tok))));
end
end
% convert to fT (?)
meg.data = meg.data * 1e15;
% apparently multiple copies of the data can be stored in the file
% representing the average, the standard deviation, etc.
% keep only the first part of the data -> average
tmp = find(diff(meg.time)<0);
if ~isempty(tmp)
meg.data = meg.data(:,1:tmp(1));
meg.time = meg.time(1:tmp(1));
meg.trigger = meg.trigger(1:tmp(1));
meg.sample = meg.sample(1:tmp(1));
end