Extracting index from cell array

9 views (last 30 days)
Khalif
Khalif on 31 Aug 2024
Commented: Umar on 1 Sep 2024
I'm currently trying to extract the indexes from a cell array of an EEG experiment and I used the ismember function to line up with the labels and extract the index number of the bad channels, but it didn't give me the results instead of just the order of the channels. Is there any solution to find the index to the corresponding channels?
{EEG.chanlocs.labels}
ans =
1×64 cell array
Columns 1 through 9
{'0Z'} {'1Z'} {'2Z'} {'3Z'} {'4Z'} {'1L'} {'1R'} {'1LB'} {'1RB'}
...
bad = split(string(dataQC(1,5))).'
1×6 string array
"1RC" "2RB" "3LD" "10L" "9L" "4LD"
[~, numchan] = ismember({EEG.chanlocs.labels}, bad)

Accepted Answer

Khalif
Khalif on 1 Sep 2024
bad = split(string({'1RC', '2RB', '3LD', '10L', '9L', '4LD'}).';
bad = convertStringsToChars(bad);
[badchan, numchan] = ismember({EEG.chanlocs.labels}, bad);
badchan = find(badchan);

More Answers (1)

Umar
Umar on 1 Sep 2024

Hi @Khalif Shirad,

To address your query regarding, “I'm currently trying to extract the indexes from a cell array of an EEG experiment and I used the ismember function to line up with the labels and extract the index number of the bad channels, but it didn't give me the results instead of just the order of the channels. Is there any solution to find the index to the corresponding channels?”

Please see my response to your comments below.

The core of the solution lies in your ismember function, which is employed to find the indexes of the bad channels within the EEG channel labels. The syntax [~, numchan] = ismember(bad, EEG.chanlocs.labels); effectively checks each element of the bad array against the EEG.chanlocs.labels array. The tilde (~) is used to ignore the first output of ismember, which indicates whether each element of bad is found in EEG.chanlocs.labels. Finally, the resulting indexes of the bad channels are displayed using the disp function. This output provides a clear view of which channels are deemed problematic based on their corresponding indexes in the original EEG channel labels array. Here is the updated code,

% Sample EEG channel labels
EEG.chanlocs.labels = {'0Z', '1Z', '2Z', '3Z', '4Z', '1L', '1R', '1LB', '1RB',
...'1RC', '2RB', '3LD', '10L', '9L', '4LD', '5Z', ...'6Z', '7Z', '8Z', '9Z',   '10Z', '11Z', '12Z', ...'13Z', '14Z', '15Z', '16Z', '17Z', '18Z', ...
                      '19Z', '20Z', '21Z', '22Z', '23Z', '24Z', ...
                      '25Z', '26Z', '27Z', '28Z', '29Z', '30Z', ...
                      '31Z', '32Z', '33Z', '34Z', '35Z', '36Z', ...
                      '37Z', '38Z', '39Z', '40Z', '41Z', '42Z', ...
                      '43Z', '44Z', '45Z', '46Z', '47Z', '48Z', ...
                      '49Z', '50Z', '51Z', '52Z', '53Z', '54Z', ...
                      '55Z', '56Z', '57Z', '58Z', '59Z', '60Z', ...
                      '61Z', '62Z', '63Z'};
% Bad channels to be identified
bad = split(string({'1RC', '2RB', '3LD', '10L', '9L', '4LD'})).';
% Find the indexes of bad channels
[~, numchan] = ismember(bad, EEG.chanlocs.labels);
% Display the results
disp('Indexes of bad channels:');
disp(numchan);

Please see attached.

Hope this helps resolve your problem. Please let me know if you have any further questions.

  2 Comments
Khalif
Khalif on 1 Sep 2024
Thank you for the help. I've found the solution by replacing the tilde (~) with a variable output and using the find function.
bad = split(string({'1RC', '2RB', '3LD', '10L', '9L', '4LD'}).';
bad = convertStringsToChars(bad);
[badchan, numchan] = ismember({EEG.chanlocs.labels}, bad);
badchan = find(badchan);
Umar
Umar on 1 Sep 2024
Hi @Khalif Shirad,
Thank you for sharing your solution with me. I appreciate your initiative in replacing the tilde (~) with a variable output and utilizing the find function. It’s always gratifying to see effective problem-solving in action. If you have any further questions or if there’s anything else I can assist you with, please don’t hesitate to reach out.

Sign in to comment.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!