function answer is displayed twice
19 次查看(过去 30 天)
显示 更早的评论
I wrote the following function:
function [maxValue] = calcMax()
%CALCMAX Summary of this function goes here
% Detailed explanation goes here
n=input('pls enter total number of values to calculate maximum: ');
vector=zeros(1,n);
for i=1:n
vector(i)= input(sprintf('Please enter value number (%d): ',i));
end
save('userVector.mat','vector');
maxValue=max(vector);
disp('Maximum value is: ');disp(maxValue);
end
When excuting it from the command window by simply typing 'calcMax', after I enter the number of values I want to calc the max for, it returns the answer twice. Once for the function request disp() and once the standard ans=...

How do I get rid of the ans= part being displayed as well?
(also if anyone can point-out how to print the answer in one line it ill be great)
Thanks in advance
0 个评论
回答(2 个)
Walter Roberson
2022-10-28
In MATLAB, when you have an expression (such as a function call) and the expression has a value and the expression does not end in a semi-colon, then MATLAB automatically displays the value of the expression.
If you want your function to be able to return a value when the function is used in a calculation or assignment statement, but you want MATLAB to not automatically display the output in the case that the function was being called in a context that did not demand an output, then you can program that behaviour into your function.
The way to detect whether your function is being used in a context that demands an output, is to test the special variable nargout which will be 0 in the case that output is not being demanded. And then when you have detected that case, do not assign anything to the named output variable -- if you assign something to the named output variable in a context where output is not being demanded then you would get the automatic output that you were observing.
You can use the nargout test to determine whether to nicely format an output for display purposes, or if instead you should skip the nice output because the function is being called as a utility routine to calculate an answer.
In order to show you the operation of nargout here in this Answers facility, I had to make another change to the function, so that it would not call input() unnecessarily. I did that by detecting whether input data was provided at the time of the function call and if so skipping the input() calls. If you call this function with no inputs then it will go back to the behaviour of prompting the user for the input values. You can tell whether a trailing variable has been passed into the function by examining the special variable nargin .
vec = [4 5 -6 -1 3]
calcMax(vec)
outputvariable = calcMax(vec)
function [maxValue_out] = calcMax(vector)
%CALCMAX Summary of this function goes here
% Detailed explanation goes here
if nargin == 0
n=input('pls enter total number of values to calculate maximum: ');
vector=zeros(1,n);
for i=1:n
vector(i)= input(sprintf('Please enter value number (%d): ',i));
end
save('userVector.mat','vector');
end
maxValue=max(vector);
if nargout == 0
disp('Maximum value is: ');disp(maxValue);
else
maxValue_out = maxValue;
end
end
Star Strider
2022-10-28
‘How do I get rid of the ans= part being displayed as well?’
Perhaps:
calcMax;
(added semicolon)
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!