Index in position 2 exceeds array bounds (must not exceed 175).

6 visualizzazioni (ultimi 30 giorni)
Hello,
I am analysing EEG data using a code that was provided to me. This bit of code was working well a year ago, and I am using it now to add new datasets.I don't understand what has changed that make it throw an error now.
The script below is used to average, plot and display the 93 EEG electrodes accross all trials for all participants. I get the following error:
Index in position 2 exceeds array bounds (must not exceed 175).
Error in butterfly_graph (line 8)
data_STD(:,:,i) = DATA{1,i}.avg(:, 1:350);
DATA is stored in a file called DataForCluster_allparticipants.mat. I don't understand what exactly is the index in position 2 that exceeds array bounds. I apologise if the answer is very obvious.
my scripts can be found here: https://we.tl/t-0ozTcZFgex
DataForCluster_allparticipants.mat that is the file where the DATA variable is stored
The script forLucie_loadDataForCluster.mat is the script generating the DATA file
The script Butterfly_graph is the script in which I have a problem
load('DataForCluster_allparticipants.mat')
Error using load
Unable to find file or directory 'DataForCluster_allparticipants.mat'.
cd('/Users/dan/Documents/LucieEEG/Analysis/Sorted') %change this
%d(1,:) is standard
%d(2,:) is deviant
% d(3,:) is MMN
for i = 1:length(DATA)
data_STD(:,:,i) = DATA{1,i}.avg(:, 1:350);
data_DEV(:,:,i) = DATA{2,i}.avg(:, 1:350);
data_MMN(:,:,i) = DATA{3,i}.avg(:, 1:350);
end
  1 Commento
Ganesh
Ganesh il 22 Giu 2024
The issue is DATA{1,15}.avg, which has dimensions 93x175.
The same can be seen in indices 15,16,17,18,19

Accedi per commentare.

Risposta accettata

Korosh Agha Mohammad Ghasemi
Spostato: Voss il 25 Giu 2024
The error you are encountering, "Index in position 2 exceeds array bounds (must not exceed 175)," typically occurs when you attempt to access an index that is out of the range of the array dimensions. This means that the array you are trying to index does not have as many columns as you expect.
From the error message, it appears that DATA{1,i}.avg does not have at least 350 columns, which is causing the error when you try to index 1:350.
Here’s a step-by-step approach to troubleshoot and fix this issue:
  1. Verify the Dimensions of DATA{1,i}.avg:First, check the size of the avg matrix in each cell of DATA. You can do this by adding a few lines of code to inspect the size before attempting to index:
for i = 1:length(DATA)
[rows, cols] = size(DATA{1,i}.avg);
fprintf('Participant %d: avg has %d rows and %d columns.\n', i, rows, cols);
end
Adjust the Indexing Based on the Actual Size:Modify the loop to handle cases where the number of columns is less than 350:
for i = 1:length(DATA)
numCols = size(DATA{1,i}.avg, 2);
colLimit = min(numCols, 350);
data_STD(:,:,i) = DATA{1,i}.avg(:, 1:colLimit);
data_DEV(:,:,i) = DATA{2,i}.avg(:, 1:colLimit);
data_MMN(:,:,i) = DATA{3,i}.avg(:, 1:colLimit);
end
  1. Check Data Consistency:Ensure that all entries in DATA have consistent dimensions. If some datasets were updated or changed, this might have introduced inconsistencies. Review the script forLucie_loadDataForCluster.m that generates the DATA file to ensure it is processing all datasets uniformly.
  2. Handle Missing Data:If some datasets are incomplete or have fewer time points, consider adding error handling or warnings to flag these cases.
Here is a more robust version of your script with additional checks:
load('DataForCluster_allparticipants.mat')
cd('/Users/dan/Documents/LucieEEG/Analysis/Sorted')
for i = 1:length(DATA)
if isfield(DATA{1,i}, 'avg')
numCols = size(DATA{1,i}.avg, 2);
colLimit = min(numCols, 350);
data_STD(:,:,i) = DATA{1,i}.avg(:, 1:colLimit);
else
warning('Missing avg field for participant %d.', i);
end
if isfield(DATA{2,i}, 'avg')
numCols = size(DATA{2,i}.avg, 2);
colLimit = min(numCols, 350);
data_DEV(:,:,i) = DATA{2,i}.avg(:, 1:colLimit);
else
warning('Missing avg field for participant %d.', i);
end
if isfield(DATA{3,i}, 'avg')
numCols = size(DATA{3,i}.avg, 2);
colLimit = min(numCols, 350);
data_MMN(:,:,i) = DATA{3,i}.avg(:, 1:colLimit);
else
warning('Missing avg field for participant %d.', i);
end
end
  1 Commento
Lucie Aman
Lucie Aman il 26 Giu 2024
THank you so much! I've unified the sampling rate of all my files and it is now working :-)

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 23 Giu 2024
Why are you taking columns 1 to 350 exactly? What if it has more or less than 350 columns? Your code is not very robust in that case.
Do you want to take ALL columns, regardless of how many there are? Then just use colon, meaning "All columns"
for i = 1:length(DATA)
data_STD(:,:,i) = DATA{1,i}.avg(:, :);
data_DEV(:,:,i) = DATA{2,i}.avg(:, :);
data_MMN(:,:,i) = DATA{3,i}.avg(:, :);
end
Or if it's more than 350 columns wide do you just want to take the first 350 columns? If so, to make it robust if there are not 350 columns do this:
numColumns = size(DATA{1,i}.avg, 2)
for i = 1:length(DATA)
data_STD(:,:,i) = DATA{1,i}.avg(:, 1 : numColumns);
data_DEV(:,:,i) = DATA{2,i}.avg(:, 1 : numColumns);
data_MMN(:,:,i) = DATA{3,i}.avg(:, 1 : numColumns);
end
  1 Commento
Lucie Aman
Lucie Aman il 26 Giu 2024
Thank you for your reply, I've made the first change you suggested using colon but it still didn't work. This helped me to realise that the problem wasn't the number of columns but it was that some EEG files were recorded at a 250 Hz sampling rate and other at 500 Hz. Now I've downsampled the 500 Hz files it's working.

Accedi per commentare.

Categorie

Scopri di più su EEG/MEG/ECoG in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by