MATLAB: Index in position 2 exceeds array bounds (must not exceed 6).

MATLABwordsearch

clear all
clc
% creates grid (horizontal)
hx0 = [0, 10]; hy0 = [0, 0];
hx1 = [0, 10]; hy1 = [1, 1];
hx2 = [0, 10]; hy2 = [2, 2];
hx3 = [0, 10]; hy3 = [3, 3];
hx4 = [0, 10]; hy4 = [4, 4];
hx5 = [0, 10]; hy5 = [5, 5];
hx6 = [0, 10]; hy6 = [6, 6];
hx7 = [0, 10]; hy7 = [7,7];
hx8 = [0, 10]; hy8 = [8,8];
hx9 = [0, 10]; hy9 = [9,9];
hx10 = [0, 10]; hy10 = [10,10];
% creates grid (vertical)
vx0 = [0, 0]; vy0 = [0, 10];
vx1 = [1, 1]; vy1 = [0, 10];
vx2 = [2, 2]; vy2 = [0, 10];
vx3 = [3, 3]; vy3 = [0, 10];
vx4 = [4, 4]; vy4 = [0, 10];
vx5 = [5, 5]; vy5 = [0, 10];
vx6 = [6, 6]; vy6 = [0, 10];
vx7 = [7, 7]; vy7 = [0, 10];
vx8 = [8, 8]; vy8 = [0, 10];
vx9 = [9, 9]; vy9 = [0, 10];
vx10 = [10, 10]; vy10 = [0, 10];
plot(hx1,hy1,'b',hx2,hy2,'b',hx3,hy3,'b',hx4,hy4,'b',hx5,hy5,'b',...
hx6,hy6,'b',hx7,hy7,'b',...
hx8,hy8,'b',hx9,hy9,'b',hx10,hy10,'b',vx0, vy0, 'b', vx1, vy1, 'b',...
vx2, vy2, 'b', vx3, vy3, 'b', vx4, vy4, 'b', vx5, vy5, 'b', vx6, vy6, 'b', ...
vx7, vy7, 'b',vx8,vy8,'b',vx9,vy9,'b',vx10,vy10,'b')
% creates alphabet array
alpha26 = {'A','B','C','D','E','F','G','H','I','J','K','L',...
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
% inputs first word
word1 = {'A','L','P','A','C','A'};
% generates random letters
for i = 1:10
for j = 1:10
ws{i,j} = alpha26{randi([1,26],1)};
end
end
search = 10;
% puts random letters in the grid
for p=1:search
for n = 1:search
te1 = text(p-0.5, n-0.5 , ws{p,n}, ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
end
search = 10;
lo = randi(10)-0.5;
po = randi(5) - 0.5;
chance=randi(2);
% puts letters of first word in the grid
for p = 1:search
for n = 1:search
if chance==1
te1 = text(lo, po , word1(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
elseif chance==2
set(te1, 'color', 'k')
te1 = text(po, lo , word1(p,n), ...
'fontsize', 20, 'horizontalalignment', 'center');
set(te1, 'color', 'k')
end
po = po + 1;
end
end
I am trying to create a 10 x 10 word search but I keep getting an error message [below] and I do not know how to fix the situation. All help is appreciated.
Index in position 2 exceeds array bounds (must not exceed 6).
Error in WordSearch (line 69)
te1 = text(po, lo , word1(p,n), ...

Best Answer

Your code:
word1 = {'A','L','P','A','C','A'};
so, word1 is a 1x6 cell array, 1 row, 6 columns
search = 10;
for p = 1:search
for n = 1:search
.. word1(p,n)
so yes, it's going to cause an index exceeds matrix dimension error, for any p greater than 1 (you go up to 10) and any n greater than 6 (again you go up to 10).
It's unclear what you're trying to do here. But clearly if you have only 6 elements, trying to access elements 7 to 100 is not going to work.
Note that if all you're going to store in your cell arrays are single letters, you'd be better off using matrices instead. It would made your life easier (but certainly wouldn't solve the above problem). e.g:
alpha26 = 'A':'Z'; %char vector
ws = alpha26(randi(26), 10, 10); %10x10 matrix of random characters
Perhaps you meant to use ws in your loop instead of word1.