Nested Loop not working

1 view (last 30 days)
Ping
Ping on 14 Oct 2012
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 = []

Accepted Answer

per isakson
per isakson on 14 Oct 2012
Edited: per isakson on 14 Oct 2012
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.
  2 Comments
Ping
Ping on 14 Oct 2012
but the first part is just the test case so I can't edit that. I edited the question to make it clearer
per isakson
per isakson on 14 Oct 2012
Edited: per isakson on 14 Oct 2012
I don't understand!
I have:
  1. fixed a few problems in your function, make_nextRow
  2. deleted blank lines and comments to keep the answer compact
  3. demonstrated that it returns numbers, which you have confirmed are indeed those expected.
What more do you expect?

Sign in to comment.

More Answers (1)

Matt J
Matt J on 14 Oct 2012
At the command line, execute
which -all make_nextRow
and see if it reports any other versions of make_nextRow existing on your path.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!