-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy patheeg_store.m
178 lines (169 loc) · 5.96 KB
/
eeg_store.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
% EEG_STORE - store specified EEG dataset(s) in the ALLEEG variable
% containing all current datasets, after first checking
% dataset consistency using EEG_CHECKSET.
%
% Usage: >> [ALLEEG EEG index] = eeg_store(ALLEEG, EEG);
% >> [ALLEEG EEG index] = eeg_store(ALLEEG, EEG, index);
%
% Inputs:
% ALLEEG - variable containing all current EEGLAB datasets
% EEG - dataset(s) to store - usually the current dataset.
% May also be an array of datasets; these will be
% checked and stored separately in ALLEEG.
% index - (optional), ALLEEG index (or indices) to use to store
% the new dataset(s). If no index is given, EEG_STORE
% uses the lowest empty slot(s) in the ALLEEG array.
% Use index of 0 for just loaded dataset.
% Outputs:
% ALLEEG - array of all current datasets
% EEG - EEG dataset (after syntax checking)
% index - index of the new dataset
%
% Note: When 3 arguments are given, after checking the consistency of
% the input dataset structures this function simply performs
% >> ALLEEG(index) = EEG;
%
% Typical use:
% >> [ALLEEG EEG CURRENTSET] = eeg_store(ALLEEG, EEG);
% creates a new dataset in variable ALLEEG.
% >> [ALLEEG EEG CURRENTSET] = eeg_store(ALLEEG, EEG, CURRENTSET);
% overwrites the current dataset in variable ALLEEG.
%
% Author: Arnaud Delorme, CNL / Salk Institute, 2001
%
% See also: EEGLAB, EEG_CHECKSET, EEG_RETRIEVE
% Copyright (C) 2001 Arnaud Delorme, Salk Institute, 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.
% store set
% ------------------
function [ALLEEG, EEG, storeSetIndex] = eeg_store(ALLEEG, EEG, storeSetIndex, varargin)
% check parameter consistency
% ------------------------------
if nargin >= 3
if length(EEG) ~= length(storeSetIndex) && storeSetIndex(1) ~= 0
error('Length of input dataset structure must equal the length of the index array');
end
end
% considering multiple datasets
% -----------------------------
if length(EEG) > 1
TMPEEG = EEG;
if nargin >= 3 && storeSetIndex(1) ~= 0
for index=1:length(TMPEEG)
EEG = TMPEEG(index);
tmpsaved = EEG.saved;
if strcmpi(tmpsaved, 'justloaded'), tmpsaved = 'yes'; end
[ALLEEG, EEG] = eeg_store(ALLEEG, EEG, storeSetIndex(index), varargin{:});
ALLEEG(storeSetIndex(index)).saved = tmpsaved;
TMPEEG(index).saved = tmpsaved;
end
else
for index=1:length(TMPEEG)
EEG = TMPEEG(index);
tmpsaved = EEG.saved;
if strcmpi(tmpsaved, 'justloaded'), tmpsaved = 'yes'; end
[ALLEEG, EEG, storeSetIndex(index)] = eeg_store(ALLEEG, EEG);
ALLEEG(storeSetIndex(index)).saved = tmpsaved;
TMPEEG(index).saved = tmpsaved;
end
end
EEG = TMPEEG;
return
end
if nargin < 3
% creating new dataset
% -> erasing file information
% ---------------------------
EEG.filename = '';
EEG.filepath = '';
EEG.datfile = '';
end
if isempty(varargin) % no text output and no check (study loading)
[ EEG, com ] = eeg_checkset(EEG);
else
com = '';
end
if nargin > 2
if storeSetIndex == 0 || strcmpi(EEG.saved, 'justloaded')
EEG.saved = 'yes'; % just loaded
else
EEG.saved = 'no';
end
elseif strcmpi(EEG.saved, 'justloaded')
EEG.saved = 'yes';
else
EEG.saved = 'no';
end
EEG = eeg_hist(EEG, com);
% find first free index
% ---------------------
findindex = 0;
if nargin < 3, findindex = 1;
elseif storeSetIndex == 0, findindex = 1;
end
if findindex
i = 1;
while (i<10000)
try
if isempty(ALLEEG(i).data)
storeSetIndex = i; i = 10000;
end
i = i+1;
catch
storeSetIndex = i; i = 10000;
end
end
if isempty(varargin) % no text output and no check
fprintf('Creating a new ALLEEG dataset %d\n', storeSetIndex);
end
else
if isempty(storeSetIndex) || storeSetIndex == 0
storeSetIndex = 1;
end
end
if ~isempty( ALLEEG )
try
ALLEEG(storeSetIndex) = EEG;
catch
allfields = fieldnames( EEG );
for i=1:length( allfields )
ALLEEG(storeSetIndex).(allfields{i}) = EEG.(allfields{i});
end
if ~isfield(EEG, 'datfile') && isfield(ALLEEG, 'datfile')
ALLEEG(storeSetIndex).datfile = '';
end
EEG = ALLEEG(storeSetIndex); % ensure equality between the two
end
else
ALLEEG = EEG;
if storeSetIndex ~= 1
ALLEEG(storeSetIndex+1) = EEG;
ALLEEG(1) = ALLEEG(storeSetIndex); % empty
ALLEEG(storeSetIndex) = ALLEEG(storeSetIndex+1);
ALLEEG = ALLEEG(1:storeSetIndex);
end
end