Index in position 2 exceeds array bounds (must not exceed 6).
Afficher commentaires plus anciens
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 commentaires
Guillaume
le 26 Mar 2019
% 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
le 26 Mar 2019
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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!