Pernicious error: ' Too many input arguments'

8 views (last 30 days)
To whom it may concern:
I have gone through many threads on MathWorks for this topic but have not found a solution.
I have a function called 'ask_for_parameters' that calls up a script containing constants called 'constants.m'. The constants contain messages corresponding to each variable and I need them to be placed in the input call of my validation function called 'ask_for_number'.
I keep getting the same error (even though there are no inputs in the functions and I have deleted all similar files and checked for correct paths):
**Error using ask_for_number Too many input arguments.
Error in ask_for_parameters (line 12) b1 = ask_for_number(STRING_B1);
MY FUNCTIONS:
function [ b1, b2, h, h1, l, v ] = ask_for_parameters()
clc;
%fprintf('Total number of inputs = %d\n',nargin)
fprintf('Insert values for each parameter. If a parameter is unknown, leave blank and press ENTER\n\n');
constants;
b1 = ask_for_number(STRING_B1);
b2 = ask_for_number(STRING_B2);
h = ask_for_number(STRING_H);
h1 = ask_for_number(STRING_H1);
l = ask_for_number(STRING_L);
v = ask_for_number(STRING_V);
end
function valid_number = ask_for_number ()
clc;
% fprintf('nb_inputs = %d\n',nargin)
valid_number = -1;
while valid_number <= 0;
txt_input = input('Enter a number\n', 's');
valid_number = str2double(txt_input);
if valid_number > 0;
end
if isnan(valid_number) || isempty(txt_input);
valid_number = [];
end
end
end
CONSTANTS SCRIPT:
STRING_B1 = 'Enter value for base 1:';
STRING_B2 = 'Enter value for base 2:';
STRING_H1 = 'Enter value for partial height:';
STRING_H = 'Enter value for total height:';
STRING_L = 'Enter value for length:';
STRING_V = 'Enter value for volume:';
Any suggestions would be greatly appreciated.

Accepted Answer

John D'Errico
John D'Errico on 7 Feb 2015
Edited: John D'Errico on 7 Feb 2015
You have defined the function as:
function valid_number = ask_for_number ()
Note that there are NO input arguments. You explicitly told MATLAB NOT to expect ANY arguments to be passed in.
Then you call it with an input argument. For example:
b1 = ask_for_number(STRING_B1);
What do you expect to see, but an error that tells you "Too many input arguments"?
**Error using ask_for_number Too many input arguments.
Error in ask_for_parameters (line 12) b1 = ask_for_number(STRING_B1);
Computers are very simple things. They do what you tell them to do. I do have a hard time understanding how this would be pernicious. Seems like you are the one who made the error, and MATLAB told you very clearly what it did not like and exactly where was the problem.

More Answers (2)

PR
PR on 7 Feb 2015
Thanks for taking the time John. However, as I am trying to make this work (as a beginner in Matlab), I merely asked for suggestions as I am stumped. Clearly I am calling an input when I specified for there to be none yet I am not sure how to proceed in this case.
  2 Comments
John D'Errico
John D'Errico on 7 Feb 2015
Edited: John D'Errico on 7 Feb 2015
Why do you feel the need to pass in an argument at all there? Are you using those strings that you are trying to pass into the function? You have defined the variable STRING_B1 (along with others) but then you never use them, except for trying to pass them into a function that does not use them either.
John D'Errico
John D'Errico on 7 Feb 2015
Edited: John D'Errico on 7 Feb 2015
There are several other issues you will trip over, like this no-op pair of statements:
if valid_number > 0;
end
(A no-op is something that does nothing, thus no operation.) Do you want it to break out of the while loop?
help break
It does nothing good or bad except dump a few CPU cycles on the floor. Anyway, the while loop itself will terminate when it hits that condition.

Sign in to comment.


PR
PR on 7 Feb 2015
My problem occurs when I call 'b1 = ask_for_number(STRING_B1)'. If I replace 'ask_for_number' with 'input' the function works and no longer gives an error for too many inputs. In the function 'ask_for_number', it calls for one input and I cannot figure out why it would trigger the error.
function [ b1 ] = ask_for_parameters()
clc;
% fprintf('Total number of inputs = %d\n',nargin)
fprintf('Insert values for each parameter. If a parameter is unknown, leave blank and press ENTER\n\n');
constants;
b1 = ask_for_number(STRING_B1);
end
  7 Comments
John D'Errico
John D'Errico on 8 Feb 2015
So, all of this begs an interesting question. And therefore, with tongue FIRMLY in cheek, I'll ask it. How many ways can we come up with to pass in a variable to a function, without ever using an argument? I can think of 4 ways, all of them firmly in the hack corner of the spectrum, IMHO.
1. Use global variables.
2. Write out a .mat file in your base workspace, then read in the file inside your function.
3. Using my uigetvar tool, as found on the file exchange, you can copy a base workspace variable into a function workspace. (This uses evalin, so any other approach that also uses evalin will not count as a new idea.) Of course, one could write a non-ui version of uigetvar. I should probably have provided that myself.
4. Write a separate function, call it hackfun. It will take an argument, then store that value as a persistent variable. Then when hackfun is called with an output argument, it will return the value stored inside.
Now I admit that all of these methods are complete hacks. Even global variables are something that I think were created in the old days of MATLAB, when better options were not available. My fervent opinion is they are best left unused.
But if you absolutely insist on not passing in an input argument, there are always ways...
Stephen23
Stephen23 on 8 Feb 2015
Edited: Stephen23 on 8 Feb 2015
5. Print the value to screen together with the instructions that the user must write it down on a piece of paper. At some point later in a different function, prompt the user to input the value via the keyboard.
6. Encode the value, bounce it via a laser off the moon (or another passing satellite or planet), and whichever function is running at the point when the signal returns has to accept the encoded value into its workspace.
7. "Print" the value to punchcards. Search for several years to find a working punchcard-reader, call the function if and only if a reader is found that can connect to the PC via USB.

Sign in to comment.

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!