In: Computer Science
In matlab please. It says that not enough arguments. How, what, and where to input?
function longestword (word1,word2,word3)
if strlength(word1) > strlength(word2) &&
strlength(word1) > strlength(word3)
fprintf(word1);
elseif strlength(word2) > strlength(word1) &&
strlength(word2) > strlength(word3)
fprintf(word2);
else
fprintf(word3)
end
end
Actually, your function longestword defines 3 input arguments (word1, word2, word3). So,once you have your longestword function defined, you need to call it in order to run the function. For example, if you call and run your longestword function in the command window without specifying any arguments like this:
longestword
then you will be getting the error "Not enough input arguments".
So basically when you have the file "longestword.m" open in the Editor, and you try to run the function by pressing the F5 or the "Run" button, MATLAB then runs the longestword function without any input arguments, and hence you get the error "Not enough input arguments". Then after this, the "Run" button dropdown menu opens up for you automatically, prompting you to enter the missing values for input arguments. There you can put your desired values and hit enter to run the function. Also, if you want to change the values of different words, you can again press the down arrow below the "Run" button and enter new values.
HOW TO FIX THE ERROR: So now when you have understood the basic reason of error, all you have to do is just call and run the longestword function with specifying 3 input arguments, "word1", "word2", and "word3" as given below (assuming that the arguments "word1", "word2", and "word3" are defined):
longestword(word1, word2, word3)
then you will not get the error message.
For your better and easy understanding, I have provided the full Matlab code for your function, so that you can run it successfully on your Matlab. The code for your longestword function is as given below:
%define variables word1, word2, and word3 before using them in our longestword function
word1 = input('Enter word 1:', 's') %define whatever string value you like for word1
word2 = input('Enter word 2:', 's') %define whatever string value you like for word2
word3 = input('Enter word 3:', 's') %define whatever string value you like for word3
%your actually defined longestword function
function longestword (word1,word2,word3)
if strlength(word1) > strlength(word2) && strlength(word1) > strlength(word3)
fprintf(word1);
elseif strlength(word2) > strlength(word1) && strlength(word2) > strlength(word3)
fprintf(word2);
else
fprintf(word3)
end
end
%avoiding and fixing the error
longestword(word1, word2, word3); %here we need to and are CALLING longestword function with three input arguments word1, word2, and word3 as defined before to avoid the Error that you are facing.
Note that I have also included comments for your easy understanding. Just read them carefully and follow the steps to avoid any errors.
Thanks!