How to suppress the ans in a user defined function
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Star Strider
2016-1-28
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 个评论
Star Strider
2016-1-28
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
2016-1-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!