MATLAB: Do I get an error message related to the number of output arguments when I run the function

argumentserrorfunctionMATLABnumberoutput

I have created a function that returns multiple output arguments:
function [x1, x2] =testfcn()
x1 = 1;
If I request a single output, it runs without any problems. However, if I request 2 outputs, I receive the following error message:
??? One or more output arguments not assigned during call to 'C:\MATLAB7\work\testfcn.m (testfcn)'.
If I request 3 or more outputs, I receive this error message:
??? Error using ==> testfcn
Too many output arguments.

Best Answer

This enhancement has been incorporated in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
An error message related to the number of output arguments of a function typically occurs when a function does not assign a value to one of its output arguments during its execution. It may also occur when requesting more output variables than the function returns, for example requesting 3 outputs when the function specifies that it only returns 2 outputs.
To resolve this issue, make sure that all output arguments of the function are assigned inside the function, as shown in the example below and never request more output variables than the function can return.
function [x1, x2] = testfcn()
x1 = 1;
x2 = 1;