subsetting from a structure array inside a for loop
Show older comments
Hello everybody, I am trying to write up a code that opens a bunch of eeglab datasets (set format), and stores a part of it for later. Here's my attempt:
conditionList={
'subj1',
'subj2',
'subj3',
'subj4'
}
chan = 'Cz' % the channel to use
for i = 1:length(conditionList)
condition = char(conditionList(i));
EEG = pop_loadset('filename', [condition '.set'], 'filepath', '~/MATLAB/');
data = squeeze(EEG(i).data( strcmpi(chan, {EEG.chanlocs.labels}), :, 1:120));
% the subset of the data we want to look at
% EEG.data structure is made of three vectors: channels, points, and trials.
end
The following error pops up: "Index exceeds matrix dimensions". I am not an expert at matlab, so any advice would be greatly appreciated!
Answers (1)
Walter Roberson
on 23 Feb 2018
You have a comment that
% EEG.data structure is made of three vectors: channels, points, and trials
which suggests that any one EEG(i).data is a scalar structure with three fields . But in the line
data = squeeze(EEG(i).data( strcmpi(chan, {EEG.chanlocs.labels}), :, 1:120));
you are treating any one EEG(i).data as if it is a 3D array. Is it really true that you have a 3D array of structures, each with three fields? Or should you perhaps be going down one layer, such as
data = squeeze(EEG(i).data.points( strcmpi(chan, {EEG.chanlocs.labels}), :, 1:120));
??
I am also concerned because you have EEG(i), indicating that EEG is a structure array, and you have {EEG.chanlocs.labels} . With EEG being a structure array, for {EEG.chanlocs.labels} to be valid, each chanlocs would have to be a scalar structure, and each labels would have to be a character vector, which would tend to suggest that each EEG(i) is only for one channel, which would argue against the possibility that EEG(i).data being a structure array with channels as a field.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!