How to suppress the ans in a user defined function

조회 수: 6 (최근 30일)
Jillian Sweatt
Jillian Sweatt 2016년 1월 28일
답변: Image Analyst 2016년 1월 28일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by