Too many input arguments

116 views (last 30 days)
Rohin
Rohin on 24 Apr 2016
Answered: Martin Vatshelle on 18 Feb 2019
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
  2 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

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

Community Treasure Hunt

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

Start Hunting!

Translated by