Too many input arguments

156 views (last 30 days)
Rohin
Rohin on 24 Apr 2016
Commented: Steven Lord on 27 Feb 2024
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 Comments
Cris LaPierre
Cris LaPierre on 19 Dec 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 on 27 Feb 2024
Like as default parametar "this" in Java?

Sign in to comment.

Answers (1)

Martin Vatshelle
Martin Vatshelle on 18 Feb 2019
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 Comment
Steven Lord
Steven Lord on 27 Feb 2024
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!