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

조회 수: 1 (최근 30일)
Nydea Ortega
Nydea Ortega 2019년 3월 26일
댓글: Walter Roberson 2019년 3월 27일
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), ...
  댓글 수: 2
Guillaume
Guillaume 2019년 3월 26일
% creates grid (horizontal)
%followed by a bunch of numbered variables (Don't number variables!)
%and some plot
How about?
xlim([0 10]);
ylim([0 10]);
grid('on');
So much simpler!
Walter Roberson
Walter Roberson 2019년 3월 26일
word1 = {'A', 'L', 'P', 'A', 'C', 'A'};
creates a 1 x 6 cell array.
search = 10;
for p = 1:search
for n = 1:search
te1 = text(lo, po, word1(p,n), ...)
end
end
tries to access the 1 x 6 cell array at indices up to (10, 10) . But only up to (1,6) exist.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2019년 3월 26일
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.
  댓글 수: 2
Nydea Ortega
Nydea Ortega 2019년 3월 27일
Is there a way in which we could delete the letters inside the existing matrix so that the letters don't overlap?
Walter Roberson
Walter Roberson 2019년 3월 27일
If you are referring to overlapping on the display, then you would need to delete the corresponding text() object.
You could, for example, keep a 10 x 10 array of handles, such as
thand = gobjects(10,10);
Then as you go to text a letter into place, you could
delete(thand(p,n));
thand(p,n) = text(...)
Though you should consider just creating the 10 x 10 array of text() with string set to blank, and then as you go to put a character there, just update the String property of the corresponding handle instead of creating a new text object.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Word games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by