-
Notifications
You must be signed in to change notification settings - Fork 708
/
openvibe_mat.m
93 lines (82 loc) · 2.93 KB
/
openvibe_mat.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
function varargout = openvibe_mat(filename, hdr, begsample, endsample, chanindx)
% OPENVIBE_MAT reads EEG data from MATLAB file with OpenVibe data that was converted
% according to http://openvibe.inria.fr/converting-ov-files-to-matlab/
%
% Use as
% hdr = openvibe_mat(filename);
% dat = openvibe_mat(filename, hdr, begsample, endsample, chanindx);
% evt = openvibe_mat(filename, hdr);
%
% See also FT_FILETYPE, FT_READ_HEADER, FT_READ_DATA, FT_READ_EVENT, QUALISYS_TSV, XSENS_MVNX
% Copyright (C) 2023 Robert Oostenveld
%
% 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$
persistent ov previous_fullname
needhdr = (nargin==1);
needevt = (nargin==2);
needdat = (nargin==5);
% use the full filename including path to distinguish between similarly named files in different directories
[p, f, x] = fileparts(filename);
if isempty(p)
% no path was specified
fullname = which(filename);
elseif startsWith(p, ['.' filesep])
% a relative path was specified
fullname = fullfile(pwd, p(3:end), [f, x]);
else
fullname = filename;
end
if isempty(previous_fullname) || ~isequal(fullname, previous_fullname) || isempty(ov)
% read the header, data and events
ov = load(fullname);
% remember the full filename including path
previous_fullname = fullname;
else
% use the persistent variable to speed up subsequent read operations
end
% the MATLAB file contains 'stims', 'sampleTime', 'samples', 'samplingFreq', 'channelNames'
[nsamples, nchans] = size(ov.samples);
if needhdr
% construct the FieldTrip header
hdr = [];
hdr.label = ov.channelNames(:);
hdr.Fs = ov.samplingFreq;
hdr.nChans = nchans;
hdr.nSamples = nsamples;
hdr.nTrials = 1; % assume continuous data
hdr.nSamplesPre = 0;
% return the header
varargout = {hdr};
elseif needdat
% select the channels and samples, and transpose the result
dat = ov.samples(begsample:endsample, chanindx)';
% return the data
varargout = {dat};
elseif needevt
evt = struct();
for i=1:size(ov.stims,1)
evt(i).type = 'trigger';
evt(i).sample = round(ov.stims(i,1)*hdr.Fs+1);
evt(i).value = ov.stims(i,2);
evt(i).offset = 0;
evt(i).duration = 0;
end
% return the events
varargout = {evt};
end % if needhdr, needdat or needevt