one of my nested functions isnt recognising a function made in another function? i know the codes messy and could be improved but i just want to make it work first

2 vues (au cours des 30 derniers jours)
function [white,red,green,orange,yellow,movie] = c1(f1,f2,f3,f4,f5,f6,f7,f8,f9,t1,t2,t3,t4,t5,t6,t7,t8,t9,r1,r2,r3,r4,r5,r6,r7,r8,r9,u1,u2,u3,u4,u5,u6,u7,u8,u9,l1,l2,l3,l4,l5,l6,l7,l8,l9,b1,b2,b3,b4,b5,b6,b7,b8,b9)
AA = [0,0,0:0,0,0:0,0,0]
BB = [1,1,1:1,1,1:1,1,1]
CC = [2,2,2:2,2,2:2,2,2]
DD = [3,3,3:3,3,3:3,3,3]
EE = [4,4,4:4,4,4:4,4,4]
FF = [5,5,5:5,5,5:5,5,5]
movie = zeros(2,50);
error = 0;
if f5 == 0
white = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
elseif f5 == 1
red = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
elseif f5 == 2
green = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
elseif f5 == 3
orange = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
elseif f5 == 4
blue = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
elseif f5 == 5
yellow = [f1,f2,f3;f4,f5,f6;f7,f8,f9];
else error = 1;
end
if t5 == 0
white = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
elseif t5 == 1
red = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
elseif t5 == 2
green = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
elseif t5 == 3
orange = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
elseif t5 == 4
blue = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
elseif t5 == 5
yellow = [t1,t2,t3;t4,t5,t6;t7,t8,t9];
else
error = 1;
end
if r5 == 0
white = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
elseif r5 == 1
red = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
elseif r5 == 2
green = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
elseif r5 == 3
orange = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
elseif r5 == 4
blue = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
elseif r5 == 5
yellow = [r1,r2,r3;r4,r5,r6;r7,r8,r9];
else
error = 1;
end
if u5 == 0
white = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
elseif u5 == 1
red = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
elseif u5 == 2
green = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
elseif u5 == 3
orange = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
elseif u5 == 4
blue = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
elseif u5 == 5
yellow = [u1,u2,u3;u4,u5,u6;u7,u8,u9];
else
error = 1;
end
if l5 == 0
white = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
elseif l5 == 1
red = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
elseif l5 == 2
green = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
elseif l5 == 3
orange = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
elseif l5 == 4
blue = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
elseif l5 == 5
yellow = [l1,l2,l3;l4,l5,l6;l7,l8,l9];
else
error = 1;
end
if b5 == 0
white = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
elseif b5 == 1
red = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
elseif b5 == 2
green = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
elseif b5 == 3
orange = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
elseif b5 == 4
blue = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
elseif b5 == 5
yellow = [b1,b2,b3;b4,b5,b6;b7,b8,b9];
else
error = 1;
end
function [x] = rename(in1,in2,in3,in4,in5,in6)
if in1(2,2) == 0
white = in1
elseif in1(2,2) == 1
red = in1
elseif in1(2,2) == 2
green = in1
elseif in1(2,2) == 3
orange = in1
elseif in1(2,2) == 4
blue = in1
elseif in1(2,2) == 5
yellow = in1
end
if in2(2,2) == 0
white = in2
elseif in2(2,2) == 1
red = in2
elseif in2(2,2) == 2
green = in2
elseif in2(2,2) == 3
orange = in2
elseif in2(2,2) == 4
blue = in2
elseif in2(2,2) == 5
yellow = in2
end
if in3(2,2) == 0
white = in3
elseif in3(2,2) == 1
red = in3
elseif in3(2,2) == 2
green = in3
elseif in3(2,2) == 3
orange = in3
elseif in3(2,2) == 4
blue = in3
elseif in3(2,2) == 5
yellow = in3
end
if in4(2,2) == 0
white = in4
elseif in4(2,2) == 1
red = in4
elseif in4(2,2) == 2
green = in4
elseif in4(2,2) == 3
orange = in4
elseif in4(2,2) == 4
blue = in4
elseif in4(2,2) == 5
yellow = in4
end
if in5(2,2) == 0
white = in5
elseif in5(2,2) == 1
red = in5
elseif in5(2,2) == 2
green = in5
elseif in5(2,2) == 3
orange = in5
elseif in5(2,2) == 4
blue = in5
elseif in5(2,2) == 5
yellow = in5
end
if in6(2,2) == 0
white = in6
elseif in6(2,2) == 1
red = in6
elseif in6(2,2) == 2
green = in6
elseif in6(2,2) == 3
orange = in6
elseif in6(2,2) == 4
blue = in6
elseif in6(2,2) == 5
yellow = in6
end
end
function [t1,t2,t3,t4,t5,t6] = clockwise(a1,a2,a3,a4,a5,a6)
t1 = [a1(3,1),a1(2,1),a1(1,1);a1(3,2),a1(2,2),a1(1,2);a1(3,3),a1(2,3),a1(1,3)]
t2 = a2
t2(1,:) = a3(1,:)
t3 = a3
t3(1,:) = a4(1,:)
t4 = a4
t4(1,:) = a5(1,:)
t5 = a5
t5(1,:) = a2(1,:)
t6 = a6
end
function [t1,t2,t3,t4,t5,t6] = anticlockwise(a1,a2,a3,a4,a5,a6)
t1 = [a1(1,3),a1(2,3),a1(3,3);a1(1,2),a1(2,2),a1(3,2);a1(1,1),a1(2,1),a1(3,1)]
t2 = a2
t2(1,:) = a5(1,:)
t3 = a3
t3(1,:) = a2(1,:)
t4 = a4
t4(1,:) = a3(1,:)
t5 = a5
t5(1,:) = a4(1,:)
t6 = a6
end
function [score] = evaluate(a,b,c,d,e,f)
j = 0
k = 0
acolour = [0,0,0,0,0,0]
bcolour = [0,0,0,0,0,0]
ccolour = [0,0,0,0,0,0]
dcolour = [0,0,0,0,0,0]
ecolour = [0,0,0,0,0,0]
fcolour = [0,0,0,0,0,0]
score = 0
while j<4
j = j + 1
while k<4
k = k + 1
if a(j,k) == a(2,2)
score = score + 10
else
acolour(a(j,k)) = 1
end
if b(j,k) == b(2,2)
score = score + 10
else
bcolour(b(j,k)) = 1
end
if c(j,k) == c(2,2)
score = score + 10
else
ccolour(c(j,k)) = 1
end
if d(j,k) == d(2,2)
score = score + 10
else
dcolour(d(j,k)) = 1
end
if e(j,k) == e(2,2)
score = score + 10
else
ecolour(e(j,k)) = 1
end
if f(j,k) == f(2,2)
score = score + 10
else
fcolour(f(j,k)) = 1
end
end
end
score = score - 10*(sum(acolour) + sum(bcolour) + sum(ccolour) + sum(dcolour) + sum(ecolour) + sum(fcolour))
j = 0
while j < 4
j = j + 1
if a(j,1) == a(j,2) == a(j,3)
score = score + 30
end
if b(j,1) == b(j,2) == b(j,3)
score = score + 30
end
if c(j,1) == c(j,2) == c(j,3)
score = score + 30
end
if d(j,1) == d(j,2) == d(j,3)
score = score + 30
end
if e(j,1) == e(j,2) == e(j,3)
score = score + 30
end
if f(j,1) == f(j,2) == f(j,3)
score = score + 30
end
j = 0
while j < 4
j = j + 1
if a(1,j) == a(2,j) == a(2,j)
score = score + 30
end
if b(1,j) == b(2,j) == b(2,j)
score = score + 30
end
if c(1,j) == c(2,j) == c(2,j)
score = score + 30
end
if d(1,j) == d(2,j) == d(2,j)
score = score + 30
end
if e(1,j) == e(2,j) == e(2,j)
score = score + 30
end
if f(1,j) == f(2,j) == f(2,j)
score = score + 30
end
end
end
end
scorecard = [0,0,0:1,0,0:1,1,0:1,2,0:1,3,0:1,4,0:1,5,0:1,6,0:1,7,0:1,8,0:1,9,0:1,10,0:1,11,0:1,12,0:2,0,0:2,1,0:2,2,0:2,3,0:2,4,0:2,5,0:2,6,0:2,7,0:2,8,0:2,9,0:2,10,0:2,11,0:2,12,0:3,0,0:3,1,0:3,2,0:3,3,0:3,4,0:3,5,0:3,6,0:3,7,0:3,8,0:3,9,0:3,10,0:3,11,0:3,12,0:4,0,0:4,1,0:4,2,0:4,3,0:4,4,0:4,5,0:4,6,0:4,7,0:4,8,0:4,9,0:4,10,0:4,11,0:4,12,0:5,0,0:5,1,0:5,2,0:5,3,0:5,4,0:5,5,0:5,6,0:5,7,0:5,8,0:5,9,0:5,10,0:5,11,0:5,12,0:6,0,0:6,1,0:6,2,0:6,3,0:6,4,0:6,5,0:6,6,0:6,7,0:6,8,0:6,9,0:6,10,0:6,11,0:6,12,0:7,0,0:7,1,0:7,2,0:7,3,0:7,4,0:7,5,0:7,6,0:7,7,0:7,8,0:7,9,0:7,10,0:7,11,0:7,12,0:8,0,0:8,1,0:8,2,0:8,3,0:8,4,0:8,5,0:8,6,0:8,7,0:8,8,0:8,9,0:8,10,0:8,11,0:8,12,0:9,0,0:9,1,0:9,2,0:9,3,0:9,4,0:9,5,0:9,6,0:9,7,0:9,8,0:9,9,0:9,10,0:9,11,0:9,12,0:10,0,0:10,1,0:10,2,0:10,3,0:10,4,0:10,5,0:10,6,0:10,7,0:10,8,0:10,9,0:10,10,0:10,11,0:10,12,0:11,0,0:11,1,0:11,2,0:11,3,0:11,4,0:11,5,0:11,6,0:11,7,0:11,8,0,11,9,0:11,10,0:11,11,0:11,12,0:12,0,0:12,1,0:12,2,0:12,3,0:12,4,0:12,5,0:12,6,0:12,7,0:12,8,0:12,9,0:12,10,0:12,11,0:12,12,0]
j = 0
while j < 170
j = j + 1
end
function [AA,BB,CC,DD,EE,FF] = moves(move,a,b,c,d,e,f)
if move == 1
clockwise(a,b,c,d,e,f)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 2
anticlockwise(a,b,c,d,e,f)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 3
clockwise(b,c,a,e,f,c)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 4
anticlockwise(b,c,a,e,f,c)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 5
clockwise(c,a,b,f,d,e)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 6
anticlockwise(c,a,b,f,d,e)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 7
clockwise(d,a,c,f,e,b)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 8
anticlockwise(d,a,c,f,e,b)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 9
clockwise(e,b,a,d,f,c)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 10
anticlockwise(e,b,a,d,f,c)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 11
clockwise(f,b,c,d,e,a)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 12
anticlockwise(f,b,c,d,e,a)
AA = t1
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
else
AA = a
BB = b
CC = c
DD = d
EE = e
FF = f
end
end
i = 0
while i < 51
i = i + 1
j = 0
while j < 158
j = j + 1
moves(scorecard(j,1),white,red,green,orange,blue,yellow)
moves(scorecard(j,2),AA,BB,CC,DD,EE,FF)
scorecard(j,3) = evaluate(AA,BB,CC,DD,EE,FF)
end
j = 0
mover = -100
while j < 158
j = j + 1
if scorecard(j,3) > mover
mover = j
end
end
moves(scorecard(j,1),white,red,green,orange,blue,yellow)
moves(scorecard(j,2),AA,BB,CC,DD,EE,FF)
rename(AA,BB,CC,DD,EE,FF)
movie(i,1) = scorecard(j,1)
movie(i,2) = scorecard(j,2)
end
displ(movie)
white
red
green
orange
blue
yellow
end
this is the code and here is the error message
Error in c1/evaluate (line 218)
if a(j,k) == a(2,2)
Error in c1 (line 416)
scorecard(j,3) = evaluate(AA,BB,CC,DD,EE,FF)
  2 commentaires
Jan
Jan le 3 Mai 2022
Modifié(e) : Jan le 3 Mai 2022
Please post the conmplete error message. This is only the part, which tells you where the problem occurs. Now consider the part explaining what the problem is.
You are right, the code is surprisingly ugly.
This line of code will most likely not do, what you expect:
if a(j,1) == a(j,2) == a(j,3)
If compares a(j,1) with a(j,2). According to the values this is TRUE or FALSE, which is treated as 1 or 0. Not 1 or 0 is compared with a(j,3).
Maybe you want:
if a(j,1) == a(j,2) && a(j,2) == a(j,3)
or
if isequal(a(j,1), a(j,2), a(j,3))
The command:
anticlockwise(f,b,c,d,e,a)
doesnot do anything. It processes the data but the output is not caught here. So what ever happens inside this function has no influence to the calling function.
What is the purpose of this block of code:
j = 0
while j < 170
j = j + 1
end
Why not just setting: j=170 ?
If you use arrays instead of a bunch of variables, the code size would be reduced by about 80%.
Oliver Bennett
Oliver Bennett le 3 Mai 2022
okay thanks i will change the statement also this is the full error code
Index in position 1 exceeds array bounds (must not exceed 1).
Error in c1/evaluate (line 218)
if a(j,k) == a(2,2)
Error in c1 (line 416)
scorecard(j,3) = evaluate(AA,BB,CC,DD,EE,FF)
i think the problem comes from the variables it is acting on is defined in a nested function so it cant access it but i dont know how to fix this

Connectez-vous pour commenter.

Réponses (1)

DGM
DGM le 3 Mai 2022
Modifié(e) : DGM le 3 Mai 2022
Since I don't even know how to call the function or what it's supposed to do, I can't really do much but point out problems and hope those are the only ones and that you'll know how to translate that evidence into a solution.
It's not an issue of access to a variable in some other scope. You're trying to access a(2,2), but a has only one row so there is no such element. Where did a come from?
function [score] = evaluate(a,b,c,d,e,f)
It was passed as an input argument. So what was it in the calling scope?
scorecard(j,3) = evaluate(AA,BB,CC,DD,EE,FF)
It was AA. You might be expecting AA to be a 3x3 matrix, but there's a couple problems.
AA = [0,0,0:0,0,0:0,0,0]
That's not a 3x3 matrix. That's a 1x7 vector. You probably meant to write
AA = [0,0,0;0,0,0;0,0,0]; % terminate lines with semicolon
That would cause the error.
Even if you change that, you'll probably still have the same error because of what precedes the call to evaluate():
% ...
moves(scorecard(j,1),white,red,green,orange,blue,yellow) % this does nothing
moves(scorecard(j,2),AA,BB,CC,DD,EE,FF) % this does nothing
scorecard(j,3) = evaluate(AA,BB,CC,DD,EE,FF)
% ...
The outputs from both calls to move() are unused.
If they were being used, then AA would be scalar instead of 3x3 or 1x7 or whatever. Why? Well, the output of moves() is ostensibly AA,BB, etc. So AA would ostentibly change. What would it change to?
function [AA,BB,CC,DD,EE,FF] = moves(move,a,b,c,d,e,f)
if move == 1
clockwise(a,b,c,d,e,f)
AA = t1 % t1 is a scalar
BB = t2
CC = t3
DD = t4
EE = t5
FF = t6
elseif move == 2
% ...
end
% ...
end
It would change to t1, which is one of the scalar input arguments to the main function.
function [white,red,green,orange,yellow,movie] = c1(f1,f2,f3,f4,f5,f6,f7,f8,f9,t1,t2,t3,t4,t5,t6,t7,t8,t9,r1,r2,r3,r4,r5,r6,r7,r8,r9,u1,u2,u3,u4,u5,u6,u7,u8,u9,l1,l2,l3,l4,l5,l6,l7,l8,l9,b1,b2,b3,b4,b5,b6,b7,b8,b9)
... but you probably expect t1 to be a 3x3 matrix because of that call to clockwise() right above it. After all, clockwise() takes that pile of named scalars and makes a pile of matrices, confusing everything by reusing variable names for different things.
function [t1,t2,t3,t4,t5,t6] = clockwise(a1,a2,a3,a4,a5,a6)
t1 = [a1(3,1),a1(2,1),a1(1,1);a1(3,2),a1(2,2),a1(1,2);a1(3,3),a1(2,3),a1(1,3)]
t2 = a2
t2(1,:) = a3(1,:)
t3 = a3
t3(1,:) = a4(1,:)
t4 = a4
t4(1,:) = a5(1,:)
t5 = a5
t5(1,:) = a2(1,:)
t6 = a6
end
Well, that might be what clockwise() is supposed to do, but its outputs are unused. So the call to clockwise() doesn't change the input argument t1 to an unrelated 3x3 matrix of the same name, so AA will be the scalar t1 that was passed to the main function.
Do with that what you will.
With a conceptual or diagrammatic explanation of the intended behavior, this could possibly be rewritten with less group effort than it would take to debug it completely and with confidence.
  1 commentaire
DGM
DGM le 3 Mai 2022
I'm going to throw this out there. Maybe you're expecting the variables to be shared between functions since they're nested, and maybe that's why you're not assigning the output of your function calls.
If so, then the way you're defining the functions is breaking that variable sharing. When you do this:
function [AA,BB,CC,DD,EE,FF] = moves(move,a,b,c,d,e,f)
you're creating new variables AA,BB, etc which exist only within the scope of this function. In order to share the same AA, etc that are used in the main function scope, don't define them anew in the nested function.
function moves(move,a,b,c,d,e,f)
I attached a copy with some edits. It almost certainly doesn't work yet, but you should see that those variables have changed color because they're being shared.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by