Pernicious error: ' Too many input arguments'

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.

 Respuesta aceptada

John D'Errico
John D'Errico el 7 de Feb. de 2015
Editada: John D'Errico el 7 de Feb. de 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.

Más respuestas (2)

PR
PR el 7 de Feb. de 2015

0 votos

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 comentarios

John D'Errico
John D'Errico el 7 de Feb. de 2015
Editada: John D'Errico el 7 de Feb. de 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 el 7 de Feb. de 2015
Editada: John D'Errico el 7 de Feb. de 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.

Iniciar sesión para comentar.

PR
PR el 7 de Feb. de 2015

0 votos

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 comentarios

(Learn to use comments. Please stop adding an answer every time you want to say something! There is a link to make a comment. Please use it.)
It triggers the error because you are passing in an argument into a function (ask_for_number) that was written to not take an argument.
Do you not see the difference between calling that function like this:
b1 = ask_for_number(STRING_B1);
and like this:
b1 = ask_for_number;
In the first case, the call attempts to pass in an argument into that function. It fails of course, because your function does not take any input arguments.
Anyway, how do you think the function ask_for_number would use that string variable that you are trying to pass in? It does not magically do something that is not in the code.
PR
PR el 7 de Feb. de 2015
If I use b1 = ask_for_number, it will not display the message I specified in STRING_B1. I'd like for ask_for_number to pass the message when user input is prompted.
John D'Errico
John D'Errico el 7 de Feb. de 2015
Editada: John D'Errico el 7 de Feb. de 2015
I thought that might be your goal. But precisely where in this function do you try to display the input argument that the function does not expect to see?
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
There is no place where you do anything of the sort. So why not change it to be vaguely like this:
function valid_number = ask_for_number(display_string)
clc;
% see what this line does:
disp(display_string)
valid_number = -1;
while valid_number <= 0;
txt_input = input('Enter a number\n', 's');
valid_number = str2double(txt_input);
if isnan(valid_number) || isempty(txt_input);
valid_number = [];
end
end
end
See that as I changed it, your function now takes an input argument, to match up with how you are calling it. It now expects to be given an argument, so there would be no surprise error generated, at least because of the input argument.
If you wanted to do so, you could instead have used that string in the input call itself, rather than displaying it separately.
If your preference is to use fprintf, I don't care. I'm just too lazy to bother with it.
Computers do as you tell them, exactly that and no more, not what you want them to do. Except in the movies, where sometimes they take over the world and try to kill us all. MATLAB is fairly benign in that respect. I hope. :)
(By the way, does anybody know when the World Domination Toolbox will be released?)
PR
PR el 7 de Feb. de 2015
Thanks John. Good suggestion; certainly the function works using disp(string), I would not have come up with that with my present hammer and chisel . However I will continue to challenge myself to write a code that relays the string to an input within the function without having an explicit input argument. Bon weekend!
Image Analyst
Image Analyst el 8 de Feb. de 2015
PR, people are supposed to put a reply to an "Answer" as a "Comment", not as a brand new, independent "Answer" to their original question. Anyway, seems like John solved it for you so now you can officially "Accept" his answer to give him credit (reputation points).
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 el 8 de Feb. de 2015
Editada: Stephen23 el 8 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

PR
el 7 de Feb. de 2015

Editada:

el 8 de Feb. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by