Too many input arguments

I am receiving the error, "Too many input arguments.", when calling a method. However, as far as I can tell, I am not passing in too many arguments.
Question: What am I doing wrong?
Observations:
  • I have 2 classes, each in their own .m file.
  • One called ConsoleApplication, and another called NeuralNetworkSolution.
  • ConsoleApplication has a static method called "run", that takes no arguments.
  • NeuralNetworkSolution has a normal method called "load_training_data", that takes 3 arguments.
Sequence of Steps that leads to the problem:
  1. First, I create an instance of NeuralNetworkSolution, in the static "run" method of ConsoleApplication.
  2. Then I call the "load_training_data" method of NeuralNetworkSolution, in the "run" method of ConsoleApplication, with the correct number of arguments.
  3. However, even though the number of arguments is correct, I still get the "Too many input arguments."
Things I've checked so far:
  • No duplicate method names. There are only two code files in the whole project, mentioned above, and only 4 methods in total, and each one is unique.
  • There are also no reserved words or toolbox functions anywhere in Matlab that are the same as either, my class names, or method names.
  • I am definitively populating the correct number of arguments.
  • When stepping through the code, I notice that by the time I step into the "load_training_data" method of NeuralNetworkSolution, the arguments that were holding the correct values back in ConsoleApplication, have now lost their values. Which leads me to consider that this may have something to do with calling a regular method from a static method. But I have not been able to identify a problem with the way I'm doing things so far.
Error Message:
Error using NeuralNetworkSolution/load_training_data
Too many input arguments.
The code looks like this:
File: ConsoleApplication.m
classdef ConsoleApplication
properties
end
methods ( Static )
function y = run()
% Create an instance of NeuralNetworkSolution.
neural_network = NeuralNetworkSolution;
% Initialize local variables.
file_path = 'R:\Projects\MATLAB\solution_1\data\source';
file_name = 'training_data.csv';
file_row_count = 57772;
% Load training data.
training_data = neural_network.load_training_data ( file_path, file_name, file_row_count );
% More code further down the method, eventually populating an output
% vector y to return to the caller, but the code breaks
% on the line above.
end
end
end
File: NeuralNetworkSolution.m
classdef NeuralNetworkSolution
properties
end
methods
function training_data = load_training_data ( file_path, file_name, file_row_count )
% Execution never reaches here. The program breaks in the static
% "run" method of ConsoleApplication.
end
end
end

3 comentarios

Rohin
Rohin el 24 de Abr. de 2016
Okay, so I just worked it out. I'll summarize the solution here for anyone else who's had this problem.
For the sake of reference, you can find the relevant material by searching for, "A Simple Class", in Documentation.
In short, for normal non-static methods, you have to add a reference object as the first argument of the argument list, or as the only argument when a method would normally not take any arguments.
So for example, if you wanted a simple method that offered no return value, and took no input arguments, then the method deceleration would look something like this, where the argument "obj" is the reference argument, which needs to be included, even if you are not going to use it.
ClassX.m
classdef ClassX
methods
function foo ( obj )
fprintf ( 'Hello World!\n' );
end
end
end
The method "foo", can be called as follows, with out any arguments.
Command Window
>> x = ClassX;
>> x.foo;
Hello World!
>>
Then, if you want actual arguments, you might do something like this, including the object reference as the first argument, even if you are not going to use it.
ClassX.m
classdef ClassX
methods
function foo ( obj, message )
fprintf ( message );
fprintf ( '\n' );
end
end
end
Then "foo", could be called with one argument as follows.
Command Window
>> x = ClassX;
>> x.foo ( 'Hello World!' );
Hello World!
>>
For multiple arguments and return values, you follow the same pattern. Just include an object reference as the first argument in the argument list, followed by all your 'actual' arguments.
ClassAdd.m
classdef ClassAdd
methods
function y = add ( obj, a, b )
y = a + b;
end
end
end
Command Window
>> f = ClassAdd;
>> f.add ( 1, 1 );
ans =
2
>>
Cris LaPierre
Cris LaPierre el 19 de Dic. de 2018
I'd recommend putting your comment in an answer and then accepting it. That way, as others search, they will see there is a solution to this question
Lav
Lav el 27 de Feb. de 2024
Like as default parametar "this" in Java?

Iniciar sesión para comentar.

Respuestas (1)

Martin Vatshelle
Martin Vatshelle el 18 de Feb. de 2019

1 voto

You are right, when you call a method of an object with the . notation, that object is considered as the first input. This is not the case for Static methods.
The difference between methods like:
classdef ClassAdd
methods
function y = add ( obj, a, b )
y = a + b;
end
end
end
And static methods
classdef ClassAddStatic
methods (Static)
function y = add ( a, b )
y = a + b;
end
end
end
is that mehods need an instance of the same class as first input, while the Static methods does not need that.
You call the methods like this:
obj = ClassAdd();
y = obj.add(a,b);
y = ClassAddStatic.add(a,b);
This is a typical example where Static method should be used.
Static methods should be used when you don't need an instance of the class as input.
But sometimes static methods can also be used with objects as input.
When you start using inheritance it gets more complicated to decide whether a method should be static or not.
I think your load method should be static as all info you need to create the object lies in the file and parameters and not in an object.
classdef ConsoleApplication
properties
end
methods ( Static )
function y = run()
% Initialize local variables.
file_path = 'R:\Projects\MATLAB\solution_1\data\source';
file_name = 'training_data.csv';
file_row_count = 57772;
% Load training data.
training_data = neural_network.load_training_data ( file_path, file_name, file_row_count );
% More code further down the method, eventually populating an output
% vector y to return to the caller, but the code breaks
% on the line above.
end
end
end
classdef NeuralNetworkSolution
properties
end
methods (Static)
function training_data = load_training_data ( file_path, file_name, file_row_count )
% Execution never reaches here. The program breaks in the static
% "run" method of ConsoleApplication.
end
end
end

1 comentario

is that mehods need an instance of the same class as first input,
This is not completely correct. Methods of a class that are not Static require an instance of the class as at least one of the inputs, but that instance need not be the first input.
There are two common cases where the object may not be the first input. The sym class defines a method plus (the function called when you use the + operator.) So does double.
x = sym('x');
Both these calls will call the plus method of the sym class, but only the first will pass the sym object x as the first input.
y = x + 1
y = 
z = 1 + x
z = 
which plus(x, 1) % equivalent of y
/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method
which plus(1, x) % equivalent of z
/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method
which plus(1, 2) % 1+2, the function used when adding doubles
built-in (/MATLAB/toolbox/matlab/ops/@double/plus) % double method
Another common case is if there are two objects in the call and there's a class precedence relationship between their two classes (one is an InferiorClasses of the other.) For example, the graph class states that objects of the class returned by the axes function is inferior to the graph class.
ax = axes;
g = graph(bucky);
This way, even if the class of ax defined a plot method, the plot method of the graph class would be called in the case below:
plot(ax, g)
That way the axes class doesn't need to know about every class that could be plotted; each class need to know how it can be plotted into an axes, which is a much more targeted piece of knowledge.
which plot(ax, g)
/MATLAB/toolbox/matlab/graphics/math/@graph/plot.m % graph method

Iniciar sesión para comentar.

Preguntada:

el 24 de Abr. de 2016

Comentada:

el 27 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by