How to suppress the ans in a user defined function
2 views (last 30 days)
Show older comments
I have been having trouble getting my code to stop outputting the ans and just output the values.
function [mn,md,mo,V,Std,minm,maxm] = mystat(T)
mn = mean(T);
md = median(T);
mo = mode(T);
V = var(T);
Std = std(T);
minm = min(T);
maxm = max(T);
fprintf('Mean = %f\nMedian = %f\nMode = %f\nVar = %f\nStd = %f\nMin = %f\nMax = %f\n',mn,md,mo,V,Std,minm,maxm)
end
0 Comments
Answers (2)
Star Strider
on 28 Jan 2016
It looks as though you have everything ‘semicoloned’, except perhaps the funciton call itself. I don’t have your code, so see if this works:
[mn,md,mo,V,Std,minm,maxm] = mystat(T);
^ <— ADDED SEMICOLON
2 Comments
Star Strider
on 28 Jan 2016
I tested it (as you posted it) with my own ‘T’ vector, and your function behaved correctly. It only produced the fprintf output to the Command Window when I ran it (in R2015b) using the function call with the semicolon.
I would have to see more of your code to figure out what the problem is. I only know it’s not with the function you posted.
Do you have another duplicate copy of your function, perhaps in another directory on your MATLAB search path, that your script is actually calling, instead of the function you posted?
Image Analyst
on 28 Jan 2016
Jillian:
This file does not produce any "ans", just the output that fprintf() makes:
function test
T = rand(1, 100);
[mn,md,mo,V,Std,minm,maxm] = mystat(T);
end
function [mn,md,mo,V,Std,minm,maxm] = mystat(T)
mn = mean(T);
md = median(T);
mo = mode(T);
V = var(T);
Std = std(T);
minm = min(T);
maxm = max(T);
fprintf('Mean = %f\nMedian = %f\nMode = %f\nVar = %f\nStd = %f\nMin = %f\nMax = %f\n',mn,md,mo,V,Std,minm,maxm)
end
The code above is all in the single file called test.m.
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!