function answer is displayed twice

19 views (last 30 days)
Barak Garty
Barak Garty on 28 Oct 2022
Commented: Star Strider on 29 Oct 2022
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

Answers (2)

Walter Roberson
Walter Roberson on 28 Oct 2022
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]
vec = 1×5
4 5 -6 -1 3
calcMax(vec)
Maximum value is: 5
outputvariable = calcMax(vec)
outputvariable = 5
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
  1 Comment
Barak Garty
Barak Garty on 29 Oct 2022
Thank you very much for this detailed answer. I am new to Matlab (I bet you can tell) so still learning. I will pay attention next function and try my best to implenent the topics you highlighted here.

Sign in to comment.


Star Strider
Star Strider on 28 Oct 2022
How do I get rid of the ans= part being displayed as well?
Perhaps:
calcMax;
(added semicolon)
.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!