MATLAB Answers

Why do I get the error '??? Output argument ‹variable› (and maybe others) not assigned during call to ‹function›.' ?

2,469 views (last 30 days)
Why do I get the following error message :
??? Output argument ‹variable› (and maybe others) not assigned during call to ‹function›.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Feb 2019
One of the functions you have called is defined to return an output argument but that output argument does not exist in the function when it tries to return.
Common causes:
You have misspelled the name of one of your output arguments inside your function, or you have forgotten to assign a value to one of the output arguments of your function. Alternatively, the function was originally written with one or more output arguments, but the section of the function that computed the output argument was removed or modified in such a way that the output argument is now extraneous.
Stop MATLAB on the last line of the function listed in the warning or error message. Verify that each of the output arguments listed in the function declaration line at the beginning of the function exist after that last line is executed (using the DBSTEP function or the Step button in the Editor). If the arguments do not exist, examine the function to determine where you intended the arguments to be declared. Verify that those lines of code are being executed and have no typographical errors.
Example demonstrating this error:

More Answers (3)

Beginner Dave
Beginner Dave on 11 Feb 2018
I also had the problem. After confirming the argument spelling, the problem persisted. I found a misplaced "end" in my function. After moving the "end" to the correct line, the problem was solved.

Mengshan Xu
Mengshan Xu on 9 Mar 2018
One more point. if you have "return" under some "if" in your function. it my cut out your process before other output arguments are assigned. It might cause such situation.
Walter Roberson
Walter Roberson on 7 May 2018
The general solution is to initialize all of your output arguments to something as the first thing you do. Then if you return() early you can be sure that they have been initialized.
It is not uncommon to encounter this kind of problem in situations in which you are checking the input arguments and if you find a problem then you return an error status in one of the arguments and then return, not having assigned to the other output arguments. That kind of situation is typically better handled by using error()
When you encounter this error, there is almost always an error in the logical of your code, a situation that you did not expect, such as having used an "if" on what you were thinking was going to be a scalar but turns out to be a vector.

Sign in to comment.

VISHNU NAIR on 17 Feb 2021
I faced the same issue. The function name must be the same as the script name. Example listed below:
Script Name: MatrixRotationx
function rotatex = rotx(x)
rotx= [1 0 0;0 cosd(x) -sind(x);0 sind(x) cosd(x)]
Note that in the above example function name "rotatex" is not same as the script name "MatrixRotationx". While running i got the error "Output argument "rotatex" (and maybe others) not assigned during call to "rotx".
When i changed the function name to Script Name "MatrixRotationx", it worked.
Hope it helps.
  1 Comment
Walter Roberson
Walter Roberson on 17 Feb 2021
no, you were assigning to the name of the function instead of to the name of the return variable. It is legal to use a different file name and function name, in which case the function is known by the file name.

Sign in to comment.


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!