MATLAB: Nested Loop not working

nested loop

The error I am getting is Error using make_nextRow (line 21) Not enough input arguments. but i've rechecked the code and I can't see anything that is wrong
here is the code, and I commented what each line is supposed to to
function rowfcn = make_nextRow(array, start_row)
%




% rowfcn = make_nextRow(array, start_row)
%
% Returns a function handle, rowfcn. When the function handle
% is firet called, it will return the row defined by start_row.
% Every call afterwards will return the next row below.
%
% If there are no more rows to return, an empty array, [], is returned.
%
% Inputs:
% array - A numerical MxN array
% start_row - An index (integer) that indicates
% which row to start returning values from
%
% Outputs:
% rowfcn - A handle to nextRow, which when alle
%
M = start_row;
rowfcn = @nextRow; %Return a handle to the NESTED function, nextRow()
end
function row = nextRow()
% Check if M is within size(array) and if it is greater than 0
if M <= array(size) && M > 0
row = array(M,:); % Get the M'th row from array and assign to row
M= M+ 1; % Increment M
else
row = [];
end
end
*this is the test case I used: *
scores = [20,90;13,56;3,67;10,78;2,54]
next_score = make_nextRow(scores,1)
next_score()
next_score()
next_score = make_nextRow(scores,4)
next_score()
next_score()
next_score()
these are the answers i'm supposed to get for my test case:
>> scores = [20,90;13,56;3,67; 10,78;2,54]
>>scores=
20 90 13 56 3 67 10 78 2 54
>> next_score = make_nextRow(scores,1)
next_score =
@make_nextRow/nextRow
>> next_score()
ans = 20 90
>> next_score()
ans = 13 56
>> next_score = make_nextRow(e7_scores,4)
next_score =
@make_nextRow/nextRow
>> next_score()
ans = 10 78
>> next_score()
ans = 2 54
>> next_score()
ans = []

Best Answer

Problems with your code:
  • nextRow() is not properly defined as a nested function but rather as a subfunction. As a consequence the input arguments, array and start_row, are "lost".
  • IMO: "M" is not an appropriate name for a row number.
  • "array(size)" should be "size( array, 1 )"
  • "nested loop" in the title is misleading
Your comments and programming style is good
I've modified the code (see below) and run
scores = [20,90;13,56;3,67;10,78;2,54];
next_score = make_nextRow( scores, 1 )
next_score()
next_score()
which returns
next_score =
@make_nextRow/nextRow
ans =
20 90
ans =
13 56
>>
where
function rowfcn = make_nextRow( array, start_row )
M = start_row;
rowfcn = @nextRow; %Return a handle to the NESTED function, nextRow()
function row = nextRow()
if M <= size( array, 1 ) && M > 0
row = array( M, : );
M= M+ 1;
else
row = [];
end
end
end
.
I don't know if that is the expected result. I let you check that.