Index in position 2 exceeds array bounds (must not exceed 175).
9 次查看(过去 30 天)
显示 更早的评论
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.
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')
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 个评论
Ganesh
2024-6-22
The issue is DATA{1,15}.avg, which has dimensions 93x175.
The same can be seen in indices 15,16,17,18,19
采纳的回答
Korosh Agha Mohammad Ghasemi
2024-6-25
移动:Voss
2024-6-25
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:
- 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
- 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.
- 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 个)
Image Analyst
2024-6-23
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 EEG/MEG/ECoG 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!