How to debug error "Index in position 1 exceeds array bounds"?

I have a cell array data. The size is 3 by 9. Every cell is the array of class double. Commented section is the size of these arrays. For example,
data{1,1}
ans =
644.1262 643.0714 646.7557 647.4521 643.0392 625.5089 623.9397 633.4525 647.2367 640.6563
422.7087 422.5670 425.1260 422.5875 420.1111 436.3631 436.7513 435.9344 487.6950 500.0021
0 0.0028 0.0056 0.0083 0.0111 0.0139 0.0167 0.0194 0.0222 0.0250
0 1.0000 0 NaN NaN NaN NaN NaN NaN NaN
When I try to run the loop to check the condition and create a new cell array with some strings, I get an error "Index in position 1 exceeds array bounds".
% cellsz = cellfun(@size,data,'uni',false)
% cellsz =
% 3×9 cell array
%
% {[4 10]} {[ 4 67]} {[4 302]} {[4 302]} {[ 4 50]} {[4 319]} {[4 118]} {[4 201]} {[4 201]}
% {[4 17]} {[4 362]} {[4 109]} {[4 253]} {[4 253]} {[ 0 0]} {[ 0 0]} {[ 0 0]} {[ 0 0]}
% {[ 4 8]} {[ 4 8]} {[4 363]} {[ 4 88]} {[4 275]} {[4 275]} {[ 4 7]} {[4 364]} {[4 364]}
D = size(data);
for i = 1 : D(1)
for j = 1 : D(2)
if data{i,j}(4,1) == 1
A{j+1,2,i} = 'Exc';
elseif data{1,3}(4,2) == 1
A{j+1,2,i} = 'Well';
else
A{j+1,2,i} = 'Bad';
end
end
end
Could you please help me to figure out what array it means? Also, what is the best way to debug such errors? Is it possible to rewrite this code to run faster? Thank you very much for your time and help! I am trying to learn how to code.

 Accepted Answer

Adam Danz
Adam Danz on 15 Sep 2021
Edited: Adam Danz on 15 Sep 2021
> " How to debug error... "
That's the right question to ask (+1).
Knowing that there would be an error, I started by setting the pause on errors (image below).
It paused on this line below where I evaluated i and j to find that the error occurs on the first iteration of both loops.
And then I saw it 👀 data{i,j}(4,1)
data{i,j} or data{1,1} accesses the first element of the data cell array, [4 10]. Notice that this has 1 row and 2 columns but you're asking for row 4, column 1. Hence the error, "Index in position 1 exceeds array bounds".
That error would also occur in the second condition, data{1,3} since none of the cell elements contain 3 rows. Rather than guessing what your intensions are, if you describe the goal I could help get you there.

18 Comments

Thank you so much for answering so well with screenshots! It's really useful!
I wasn't clear enough: [4 10] is the size of the array in this cell. The array itself in data{1,1} is
data{1,1}
ans =
644.1262 643.0714 646.7557 647.4521 643.0392 625.5089 623.9397 633.4525 647.2367 640.6563
422.7087 422.5670 425.1260 422.5875 420.1111 436.3631 436.7513 435.9344 487.6950 500.0021
0 0.0028 0.0056 0.0083 0.0111 0.0139 0.0167 0.0194 0.0222 0.0250
0 1.0000 0 NaN NaN NaN NaN NaN NaN NaN
I want to check the forth raw every array in the cell of cell array data. It has three possibility: 100, 010 and 001.
data{1,1}(4,:)
ans =
0 1 0 NaN NaN NaN NaN NaN NaN NaN
Then I wanted to create new cell array (which I will write to xlsx file) with i sheets (third dimension of data array), 2 columns and j+1 raws. I start to fill it in with second raw (first raw is empty for now) and second column for i=1, j=1. Then third raw for i=1, j=2, etc. When i=2, I will switch to the next imaginary excel sheet. However, instead of 100, 010 and 001 I want to write down these strings:
100 - Exc
010 - Well
001 - Bad
If you follow the instructions in my answer and pause-on-error, you can evaluate i and j to determine which iterations are causing the problem. Then you can investigate the cell array element:
size(data{i,j})
My bet is that one of the matrices in the cell array has less than 4 rows.
Globally, I want to create a code that would analyze excel data files and create one excel file with the summary.
I download all the excel files in the folder in one object data, which is a cell array. Dimensions 3x9 mean that it was three files with nine sheets. And then every cell is the matrix of values specific excel document has on specific sheet.
For example, data{1,1} is the array the first excel document has on a first sheet.
Thank you so much! Your answers are very helpful!
I still don't quite understand the task.
Does each element of the cell array contain the same number of columns?
Are the first columns the only values that matter or can the 100/010/001 patterns be anywhere within the 4th row?
Oh, you are right! I have empty cells with basically dimensions (0,0)!
These are the dimensions of arrays inside the cell array.
3×9 cell array
%
% {[4 10]} {[ 4 67]} {[4 302]} {[4 302]} {[ 4 50]} {[4 319]} {[4 118]} {[4 201]} {[4 201]}
% {[4 17]} {[4 362]} {[4 109]} {[4 253]} {[4 253]} {[ 0 0]} {[ 0 0]} {[ 0 0]} {[ 0 0]}
% {[ 4 8]} {[ 4 8]} {[4 363]} {[ 4 88]} {[4 275]} {[4 275]} {[ 4 7]} {[4 364]} {[4 364]}
{[4 10]} - means I have there an array 4 by 10. {[4 362]} - array's dimensions in data{2,2}.
As you correctly guessed, in the second raw I have empty cells (one file had just five sheets and it automatically just created these empty cells. How do I get rid of these empty cells? Do I need to put the condition to check if the cell is empty?
This is how the 4th raw looks like
ans =
0 1 0 NaN NaN NaN NaN NaN NaN NaN
It's always 3 first values, and all other NaN.
> How do I get rid of these empty cells?
data(cellfun('isempty',data)) = [];
To extract the first 3 values of each 4th-row,
V = cellfun(@(x){x(4,1:3)},data)';
V will be a nx3 cell array of 1x3 vectors for n ements of the 'data' cell array.
Then, to conver the binary codes to strings,
str = strings(size(V));
str(cellfun(@(x)isequal(x, [1 0 0]), V)) = "Exc";
str(cellfun(@(x)isequal(x, [0 1 0]), V)) = "Well";
str(cellfun(@(x)isequal(x, [0 0 1]), V)) = "Bad";
Example:
base = {[1 0 0]; [0 1 0]; [0 0 1]};
V = base(randi(3,10,1))
V = 10×1 cell array
{[0 0 1]} {[0 0 1]} {[0 1 0]} {[0 0 1]} {[1 0 0]} {[1 0 0]} {[0 0 1]} {[0 0 1]} {[0 1 0]} {[0 0 1]}
str = strings(size(V));
str(cellfun(@(x)isequal(x, [1 0 0]), V)) = "Exc";
str(cellfun(@(x)isequal(x, [0 1 0]), V)) = "Well";
str(cellfun(@(x)isequal(x, [0 0 1]), V)) = "Bad"
str = 10×1 string array
"Bad" "Bad" "Well" "Bad" "Exc" "Exc" "Bad" "Bad" "Well" "Bad"
I never like combining cell array braces and parentheses. It's hard to know what you're going to get, and hard to debug situations like you've just encountered. So instead of
if data{1,1}(4,:) == 1
I'd do
cellContents = data{1,1}; % A 2-D matrix, we hope.
if cellContents(4, 1) == 1
It's much easier to debug that way.
> It's hard to know what you're going to get
I've been thinking about this and cannot come up with an example where the output is not predicable with C{i,j}(m,n) but is predictable with var=C{i,j};var(m,n). Is there an example you've found that could clear that up? I can see how extracting the cell element to a variable may be helpful to read for some people, though, and I can see how it would help with debugging.
Thank you so much for all your comments! I am trying it right now!
Could you please clarify for me your last question? Not sure if I understood. Why do I use cell arrays?
Since my goal is to analyze the excel files in the folder and create a new summary excel file with multiple sheets (where each sheet has a name of original excel files), I first created the object data which contains all the information. Do you think it's better and faster not to do that and just analyze every file separately?
It's best (faster) to read in your input workbooks one at a time, then somehow process that data to add it onto a growing output data variable, and then once all of the input workbooks have been read in and processed, do a single write of your output variable(s) to a file.
Thank you very much for all your answers! Just one more quetion.
The code
data(cellfun('isempty',data)) = [];
does change the structure of data, after applying it, data becomes 1x23 cell array, and I can't distinguish now between different input files. Just curious, if it's possible to get rid of empty cells without changing the array structure.
Thank you!
You could replace the empty cells with a matrix of NaN values. Then the V variable in my comment above will contain the NaN values extracted from row 4.
data(cellfun('isempty',data)) = {nan(4)};
Replacing worked well!
After getting rid of these empty arrays, I tried my code just for the interest.
Just to repeat
D = size(data);
for i = 1 : D(1)
for j = 1 : D(2)
if data{i,j}(4,1) == 1
A{j+1,2,i} = 'Exc';
elseif data{1,3}(4,2) == 1
A{j+1,2,i} = 'Well';
else
A{j+1,2,i} = 'Bad';
end
end
end
A is cell array that looks like this
val(:,:,1) =
{0×0 double} {0×0 double}
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
val(:,:,2) =
{0×0 double} {0×0 double}
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
val(:,:,3) =
{0×0 double} {0×0 double}
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
{0×0 double} {'Well' }
How to I catch these mistakes and correct them, when code runs smoothly and doesn't give any warnings or errors? (it looks like it runs correctly, but if I have loop in a loop, what is the best way to catch the errors?)
Also, it was the first time for me when I saw the function handle in a code you suggested
V = cellfun(@(x){x(4,1:3)},data)';
Does x(4, 1:3) is a function, basically?
Thank you!
I wouldn't use a loop. I would use the approach demonstrated in this comment above.
The empties in the first row are because you aren't writing any data to the first row.
A{j+1,2,i}x
When j==1, your are writing to row 2.
Also, what's up with the "2" ? Get rid of that.
Also, should the i and j be switched? i represents the rows, j represents the columns of data. But in your loop, it's the other way around.
Thank you very much for your help! I understood!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Asked:

on 15 Sep 2021

Commented:

on 21 Sep 2021

Community Treasure Hunt

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

Start Hunting!