[422372]: / functions / popfunc / pop_importegimat.m

Download this file

162 lines (147 with data), 6.8 kB

  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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
% POP_IMPORTEGIMAT - import EGI Matlab segmented file
%
% Usage:
% >> EEG = pop_importegimat(filename, srate, latpoint0);
%
% Inputs:
% filename - Matlab file name
% srate - sampling rate
% latpoint0 - latency in sample ms of stimulus presentation.
% When data files are exported using Netstation, the user specify
% a time range (-100 ms to 500 ms for instance). In this
% case, the latency of the stimulus is 100 (ms). Default is 0 (ms)
%
% Output:
% EEG - EEGLAB dataset structure
%
% Authors: Arnaud Delorme, CERCO/UCSD, Jan 2010
% Copyright (C) Arnaud Delorme, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [EEG com] = pop_importegimat(filename, srate, latpoint0, dataField);
EEG = [];
com = '';
if nargin < 3, latpoint0 = 0; end
if nargin < 4, dataField = 'Session'; end
if nargin < 1
% ask user
[filename, filepath] = uigetfile('*.mat', 'Choose a Matlab file from Netstation -- pop_importegimat()');
if filename == 0
return;
end
filename = fullfile(filepath, filename);
tmpdata = load('-mat', filename);
fieldValues = fieldnames(tmpdata);
sessionPos = strmatch('Session', fieldValues);
posFieldData = 1;
if ~isempty(sessionPos), posFieldData = sessionPos; end
if ~isfield(tmpdata, 'samplingRate'), srate = 250; else srate = tmpdata.samplingRate; end
% epoch data files only
promptstr = { { 'style' 'text' 'string' 'Sampling rate (Hz)' } ...
{ 'style' 'edit' 'string' int2str(srate) } ...
{ 'style' 'text' 'string' 'Sample latency for stimulus (ms)' } ...
{ 'style' 'edit' 'string' '0' } ...
{ 'style' 'text' 'string' 'Field containing data' } ...
{ 'style' 'popupmenu' 'string' fieldValues 'value' posFieldData } ...
};
geometry = { [1 1] [1 1] [1 1] };
result = inputgui( 'geometry', geometry, 'uilist', promptstr, ...
'helpcom', 'pophelp(''pop_importegimat'')', ...
'title', 'Import a Matlab file from Netstation -- pop_importegimat()');
if isempty(result)
return;
end
srate = str2num(result{1});
latpoint0 = str2num(result{2});
dataField = fieldValues{result{3}};
if isempty(latpoint0), latpoint0 = 0; end
end
EEG = eeg_emptyset;
fprintf('Reading EGI Matlab file %s\n', filename);
tmpdata = load('-mat', filename);
if isfield(tmpdata, 'samplingRate') % continuous file
srate = tmpdata.samplingRate;
end
fieldValues = fieldnames(tmpdata);
if all(cellfun(@(x)isempty(findstr(x, 'Segment')), fieldValues))
EEG.srate = srate;
indData = strmatch(dataField, fieldValues);
EEG.data = tmpdata.(fieldValues{indData(1)});
EEG = eeg_checkset(EEG);
EEG = readegilocs(EEG);
com = sprintf('EEG = pop_importegimat(''%s'');', filename);
else
% get data types
% --------------
allfields = fieldnames(tmpdata);
for index = 1:length(allfields)
allfields{index} = allfields{index}(1:findstr(allfields{index}, 'Segment')-2);
end
datatypes = unique_bc(allfields);
datatypes(cellfun(@isempty, datatypes)) = [];
% read all data
% -------------
counttrial = 1;
EEG.srate = srate;
latency = (latpoint0/1000)*EEG.srate+1;
for index = 1:length(datatypes)
tindex = 1;
for tindex = 1:length(allfields)
if isfield(tmpdata, sprintf('%s_Segment%d', datatypes{index}, tindex))
datatrial = getfield(tmpdata, sprintf('%s_Segment%d', datatypes{index}, tindex));
if counttrial == 1
EEG.pnts = size(datatrial,2);
EEG.data = repmat(single(0), [size(datatrial,1), size(datatrial,2), 1000]);
end
EEG.data(:,:,counttrial) = datatrial;
EEG.event(counttrial).type = datatypes{index};
EEG.event(counttrial).latency = latency;
EEG.event(counttrial).epoch = counttrial;
counttrial = counttrial+1;
latency = latency + EEG.pnts;
end
end
end
fprintf('%d trials read\n', counttrial-1);
EEG.data(:,:,counttrial:end) = [];
EEG.setname = filename(1:end-4);
EEG.nbchan = size(EEG.data,1);
EEG.trials = counttrial-1;
if latpoint0 ~= 1
EEG.xmin = -latpoint0/1000;
end
EEG = eeg_checkset(EEG);
% channel location
% ----------------
if all(EEG.data(end,1:10) == 0)
disp('Deleting empty data reference channel (reference channel location is retained)');
EEG.data(end,:) = [];
EEG.nbchan = size(EEG.data,1);
EEG = eeg_checkset(EEG);
end
EEG = readegilocs(EEG);
com = sprintf('EEG = pop_importegimat(''%s'', %3.2f, %3.2f, %d);', filename, srate, latpoint0, dataField);
end