Indexing problem: Unable to use a value of type cell as an index

2 views (last 30 days)
Hello,
I have collected EEG data from multiple participants and created an Excel table with the trials that I rejected. Now I want to remove the same trials from the stimuli data. The rejected trials are different for each subject and are categorised into three columns, corresponding to (rejected_p0, rejected_p1, rejected_p2).
For this I am using a for loop to load the stimuli data of each subject (downsamp_audio.mat) and replace the trials with an empty vector (downsamp_audio_rejctp1, [] ). My problem is with how to index the trials I want to remove. I want to get the trials from my excel table into a new structure (rejectrl.p1) and use that as an index inside the loop.
Is there a way to save the trials numbers in a vector or structure that can be used for indexing? And not as cell, since that doesn't work.
subject = ["subj1"; "subj2"; "subjx"]
%Read excel table containing the rejected trials for each subject, split
%into three columns (part0, part1, part2)
rejecTrials_all = table2struct(readtable('/TablewithTrialstobeRemoved.xlsx'), 'ToScalar', true);
%get rejected trials only from part1 into a separate structure
rejectrl.p1 = rejctTrials_all.rejected_p1;
% Run for loop and remove rejected trials from the stimuli files of each subject
for subj = 1:length(subjects)
%load stimuli files
load(fullfile(audioDIR, [subjects{subj} '_downsamp_audio.mat']), 'downsamp_audio');
for i = 1:length(downsamp_audio)
downsamp_audio_rejctp1 = downsamp_audio;
downsamp_audio_rejctp1(rejectrl(subj).p1) = []; %replace the bad trials with empty vector in the stimulus files
%Here is where I get the ERROR
end
save(fullfile(outDIR, [subjects{subj} '_downsamp_audio_rejctp1.mat']),'downsamp_audio_rejctp1', '-v7.3');
end
Would be thankful for any suggestions!

Answers (1)

Ayush
Ayush on 3 Oct 2023
To save the trials numbers in a vector you can use “cell2mat” function.
% Load the Excel table containing rejected trials
rejected_trials = xlsread('path_to_your_excel_file.xlsx');
% Extract the trial numbers for rejected_p1
rejected_p1 = rejected_trials(:, 2); % Assuming rejected_p1 is in the second column
% Convert the cell array to a numeric array
rejected_p1 = cell2mat(rejected_p1);
To read more about the “cell2mat” function kindly view the following documentation.
Thank you.
Hope this helps!

Categories

Find more on Data Import and Analysis 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!