/
ft_fetch_header.m
114 lines (102 loc) · 3.74 KB
/
ft_fetch_header.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function [hdr] = ft_fetch_header(data)
% FT_FETCH_HEADER mimics the behavior of FT_READ_HEADER, but for a FieldTrip
% raw data structure instead of a file on disk.
%
% Use as
% hdr = ft_fetch_header(data)
%
% See also FT_READ_HEADER, FT_FETCH_DATA, FT_FETCH_EVENT
% Copyright (C) 2008, Esther Meeuwissen
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% check whether input is data
data = ft_checkdata(data, 'datatype', 'raw', 'hassampleinfo', 'yes');
trlnum = length(data.trial);
trllen = zeros(trlnum,1);
for trllop=1:trlnum
trllen(trllop) = size(data.trial{trllop},2);
end
if isfield(data, 'sampleinfo')
% construct the trial definition according to thew samples from the original data file
trl = data.sampleinfo;
trl(:,3) = 0;
else
% construct the trial definition as if it is a continuous piece of data
trl = [1 sum(trllen) 0];
end
% fill in some header details
hdr.Fs = data.fsample;
hdr.label = data.label(:);
hdr.nChans = numel(data.label);
% fill in the channel type
if isfield(data, 'chantype')
hdr.chantype = data.chantype(:);
elseif isfield(data, 'hdr') && isfield(data.hdr, 'chantype')
% keep them ordered according to the FieldTrip data structure, which might differ from the original header
[datindx, hdrindx] = match_str(data.label, data.hdr.label);
hdr.chantype = repmat({'unknown'}, hdr.nChans, 1);
hdr.chantype(datindx) = data.hdr.chantype(hdrindx);
else
% try to determine them on the basis of heuristics
hdr.chantype = ft_chantype(data);
end
% fill in the channel unit
if isfield(data, 'chanunit')
hdr.chanunit = data.chanunit(:);
elseif isfield(data, 'hdr') && isfield(data.hdr, 'chanunit')
% keep them ordered according to the FieldTrip data structure, which might differ from the original header
[datindx, hdrindx] = match_str(data.label, data.hdr.label);
hdr.chanunit = repmat({'unknown'}, hdr.nChans, 1);
hdr.chanunit(datindx) = data.hdr.chanunit(hdrindx);
else
% try to determine them on the basis of heuristics
hdr.chanunit = ft_chanunit(data);
end
% retain the original header details
if isfield(data, 'hdr') && isfield(data.hdr, 'orig')
hdr.orig = data.hdr.orig;
end
% determine hdr.nSamples, hdr.nSamplesPre, hdr.nTrials
% always pretend that it is continuous data
hdr.nSamples = max(trl(:,2));
hdr.nSamplesPre = 0;
hdr.nTrials = 1;
% retrieve the gradiometer and/or electrode and/or optode information
if isfield(data, 'grad')
hdr.grad = data.grad;
elseif isfield(data, 'hdr') && isfield(data.hdr, 'grad')
hdr.grad = data.hdr.grad;
end
if isfield(data, 'elec')
hdr.elec = data.elec;
elseif isfield(data, 'hdr') && isfield(data.hdr, 'elec')
hdr.elec = data.hdr.elec;
end
if isfield(data, 'opto')
hdr.opto = data.opto;
elseif isfield(data, 'hdr') && isfield(data.hdr, 'opto')
hdr.opto = data.hdr.opto;
end
% retrieve the synchronization information
if isfield(data, 'hdr') && isfield(data.hdr, 'FirstTimeStamp')
hdr.FirstTimeStamp = data.hdr.FirstTimeStamp;
end
if isfield(data, 'hdr') && isfield(data.hdr, 'TimeStampPerSample')
hdr.TimeStampPerSample = data.hdr.TimeStampPerSample;
end