0
votes

I made a simple function that loops between the rows and columns of an array using for loops. The loop is part of a function named checktakentest (Since I'm testing this method atm). I keep getting the error that there aren't enough input arguments.

function [spotTaken] = checktakentest(tttArray)
for h = 1:3
    if tttArray(h,j) == 1
    %Is spot is taken, break loop
        spotTaken = 1; break;
    else
        spotTaken = 0;
    end
    for j=1:3
        if tttArray(h,j) == 1
            spotTaken = 1; break;
        else
            spotTaken = 0;
        end
    end
end

I tried also defining h and j previously as follows

 h = [1,2,3];
 j = [1,2,3];

Note that tttArray is a global variable defined in another function and its array values change in that function. A spot taken is 1, empty is 0. What arguments should I pass to the function and how do I know which ones to pass since this has been a recurring problem for me? A simple explanation would be appreciated. Note that I call the function via

checktakentest(tttArray)
1

1 Answers

2
votes

Just remove the first if clause - at that point you don't have j initialized to a value, so you can't use it, yet:

function [spotTaken] = checktakentest(tttArray)
for h = 1:3
    for j=1:3
        if tttArray(h,j) == 1
            spotTaken = 1; break;
        else
            spotTaken = 0;
        end
    end
end

If you call your function like this: checktakentest(tttArray) with tttArray beeing a mxn-matrix with m>2 and n>2 you should not get an error.

If you call it like this: checktakentest you will get the error you described (not enough input arguments).