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)
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
t(i,:) = padarray(output{i,2},[0,7-ns(i)],0,'post');
a(i,:) = padarray(output{i,3},[0,7-ns(i)],0,'post');
p(i,:) = padarray(output{i,4},[0,7-ns(i)],0,'post');
w(i,:) = padarray(output{i,5},[0,7-ns(i)],0,'post');
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')

Answers (1)

Voss
Voss 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.

Community Treasure Hunt

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

Start Hunting!