# Why does MATLAB think I have 5 output args when I have 6?

1 view (last 30 days)
Alex on 18 Nov 2014
Edited: Adam on 18 Nov 2014
I need help diagnosing this. I have a function that was working when I had 5 output arguments and then I added an output argument and saved it and it no longer works. When I run nargout I get 5 when I clearly have 6. Not sure what is going on here. Please help. code below.
function [RMS_before_YPR,RMS_after_YPR,YPR,totalRMS_before,totalRMS_after,visor_corrected] = visor_comp(visor1,visor2);
%%Save original data, sort and match data
% orig1=visor1;
% orig2=visor2;
[visor1,visor2] = organize_data(visor1,visor2);
%%Create and apply geometric transformation (YPR) to visor1
if isempty(visor1)
% [RMS_before_YPR,RMS_after_YPR,YPR,totalRMS]
RMS_before_YPR=0;
RMS_after_YPR=0;
YPR=0;
totalRMS_before=0;
totalRMS_after=0;
else
num=length(visor1);
tform=fitgeotrans(visor1,visor2,'nonreflectivesimilarity');
rot_cos=tform.T(1,1);
rot_sin=tform.T(2,1);
roll=atan2d(rot_sin,rot_cos);
yaw=tform.T(3,1);
pitch=tform.T(3,2);
visor_corrected=transformPointsForward(tform,visor1);
RMS_before_YPR = (sqrt(sum((visor2-visor1).^2)./num).*pi./180).*1e3;
sum_of_sqs1=sum((visor2(:,1)-visor1(:,1)).^2+(visor2(:,2)-visor1(:,2)).^2)./num;
totalRMS_before=(sqrt(sum_of_sqs1).*(pi/180)).*1e3;
RMS_after_YPR = (sqrt(sum((visor2-visor_corrected).^2)./num).*pi./180).*1e3;
sum_of_sqs2=sum((visor2(:,1)-visor_corrected(:,1)).^2+(visor2(:,2)-visor_corrected(:,2)).^2)./num;
totalRMS_after=(sqrt(sum_of_sqs2).*(pi/180)).*1e3;
YPR=[yaw,pitch,roll];
end
Matt on 18 Nov 2014
Alex,
The code seems to run and nargout returns a value of 6 if six outputs are in the calling statement.
Two things.
A) Make sure that when you call the function, you are asking for all 6 outputs. nargout tells you the number of outputs asked for - not the number of outputs available.
B) Make sure you define your outputs in all execution paths. Right now if visor1 is empty then visor_corrected is never populated.

Edited: Adam on 18 Nov 2014
Have you used mlock on the file at all?
Your comment below shows it thinks you have too many arguments when you call it, i.e. it is still expecting just 5 output arguments.
Type
mislocked( visor_comp );
If it returns 1 then type
munlock( visor_comp );
If it doesn't return true then just try:
clear all;
and if that doesn't work try restarting Matlab. I have occasionally had old things hang around in memory (though usually classes rather than functions) until I restart Matlab.
Alex on 18 Nov 2014
Adam, Thanks. This wasn't quite it but you put me on the path to getting it solved. Stupid error. I apparently had the file in some other path than my matlab folder and matlab was shadowing the file. Once I added the path everything worked. Looking at locking the memory was a good suggestion.

Ken Atwell on 18 Nov 2014
Do you mean that you are calling nargout from within the function, maybe inside the debugger? If so, nargout will be the number of outputs the caller is capturing, not the total number that could be returned. It works this way so a function can avoid computing an output argument that the caller is not capturing.
'nargout' would be 2 in this case:
>> [a,b] = visor_comp(0,0)
So, check where you are calling this code from and be sure you're capturing all six output.

Matt on 18 Nov 2014
Alex,
Two things.
A) Make sure that when you call the function, you are asking for all 6 outputs. nargout tells you the number of outputs asked for - not the number of outputs available.
B) Make sure you define your outputs in all execution paths. Right now if visor1 is empty then visor_corrected is never populated.
Alex on 18 Nov 2014
Sorry I should have been more clear in my description. When I type this
[RMS_before_YPR,RMS_after_YPR,YPR,totalRMS_before,totalRMS_after,visor_corrected] = visor_comp(visor1,visor2);
I get this error
Error using visor_comp
Too many output arguments.
Then right after that I type in the command window
>> nargout('visor_comp')
ans =
5
This makes no sense to me, because clearly I have entered 6 outputs and as far as I can tell in my code they are all accounted for and should be output. Do you see any reason why I am getting 5 and this error?