This feels like a really dumb question because the error message is really straight forward, so I took a while to create as simple of a complete example as I could. It seems like when a constructor is called in a constructor ( I think I first noticed in a normal method not a constructor) and the objects are being put into an array in reverse order then matlab will give a not enough input arguments error
for example:
classdef practice
methods
function self = practice(b)
b
if b>1
for i = 2:-1:1
s(i) = practice(b-i);
end
end
end
end
end
called as
practice(4)
gives
b =
4
b =
2
b =
0
Error using practice (line 4)
Not enough input arguments.
Error in practice (line 7)
s(i) = practice(b-i);
Error in practice (line 7)
s(i) = practice(b-i);
This case is odd because it only fails when getting to where b <= 1, but my real code doesn't fail like this. Any ideas on what exactly is going wrong and how I can fix it?
I think the problem is on the first iteration, when b = 4
, you set s(i) = practice(b-i)
and i = 2
. This will initialize s
to an array of practice
of length
2, but since you initialize the second element first, the first element will get initialized to a default practice
, where input b
is undefined.
If you change the indices over i
iterates to i = 1:2
this should fix the problem.
Edit:
For example, try clearing your workspace and then doing:
s(2) = practice(0)
This will attempt to assign practice(0)
to s(2)
and then s(1)
will get assigned a default initialized practice
, in which case b
is not defined. This will replicate the problem you're having. The solution is to assign to s(1)
first, and then s(2)
next.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments