MATLAB Language Common mistakes and errors Not enough input arguments


Often beginning MATLAB developers will use MATLAB's editor to write and edit code, in particular custom functions with inputs and outputs. There is a Run button at the top that is available in recent versions of MATLAB:

enter image description here

Once the developer finishes with the code, they are often tempted to push the Run button. For some functions this will work fine, but for others they will receive a Not enough input arguments error and be puzzled about why the error occurs.

The reason why this error may not happen is because you wrote a MATLAB script or a function that takes in no input arguments. Using the Run button will run a test script or run a function assuming no input arguments. If your function requires input arguments, the Not enough input arguments error will occur as you have written a functions that expects inputs to go inside the function. Therefore, you cannot expect the function to run by simply pushing the Run button.

To demonstrate this issue, suppose we have a function mult that simply multiplies two matrices together:

function C = mult(A, B)
    C = A * B;

In recent versions of MATLAB, if you wrote this function and pushed the Run button, it will give you the error we expect:

>> mult
Not enough input arguments.

Error in mult (line 2)
    C = A * B;

There are two ways to resolve this issue:

Method #1 - Through the Command Prompt

Simply create the inputs you need in the Command Prompt, then run the function using those inputs you have created:

A = rand(5,5);
B = rand(5,5);
C = mult(A,B);

Method #2 - Interactively through the Editor

Underneath the Run button, there is a dark black arrow. If you click on that arrow, you can specify the variables you would like to get from the MATLAB workspace by typing the way you want to call the function exactly as how you have seen in method #1. Be sure that the variables you are specifying inside the function exist in the MATLAB workspace: