How to suppress the ans in a user defined function

3 次查看(过去 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

回答(2 个)

Star Strider
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 个评论
Jillian Sweatt
Jillian Sweatt 2016-1-28
The T is an array of user defined values. After adding a semicolon, the function still returns ans as it had before adding the semicolon.
Star Strider
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
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.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by