# Getting "Index exceeds array bounds" for line 103 but I do not see how the index number is more than the elements. Any help would be awesome!

1 view (last 30 days)
Tyler Allcroft on 19 Jan 2022
Edited: DGM on 19 Jan 2022
for i = 2:length(output)
if strcmp(output{i,6},'Yes') || strcmp(output{i,6},'Cancel')
ns(i) = output{i,1};
if ns(i)>7
t(i,:) = zeros(1,7);
a(i,:) = zeros(1,7);
p(i,:) = zeros(1,7);
w(i,:) = zeros(1,7);
else
end
if strcmp(output{i,6},'Yes')
quality(i) = 1;
else
quality(i) = 2;
end
end
end
>> DataAnalysisPipeline
Index exceeds array bounds.
Error in DataAnalysisPipeline (line 103)
if strcmp(output{i,6},'Yes') || strcmp(output{i,6},'Cancel')

_ on 19 Jan 2022
Assuming output is an n-by-6 cell array, length(output) will be n if n >= 6 and 6 otherwise (i.e., if n < 6). Note that length() gives the size of the array in the longest dimension.
output = cell(10,6);
length(output)
ans = 10
output = cell(4,6);
length(output)
ans = 6
If you want to iterate over rows of output, you can use for i = 2:size(output,1)
output = cell(10,6);
size(output,1)
ans = 10
output = cell(4,6);
size(output,1)
ans = 4
Another possibility for why this error is happening would be that output has at least 6 rows (so that using length() is ok in this case) but fewer than 6 columns.