How to suppress the ans in a user defined function

6 Ansichten (letzte 30 Tage)
Jillian Sweatt
Jillian Sweatt am 28 Jan. 2016
Beantwortet: Image Analyst am 28 Jan. 2016
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

Antworten (2)

Star Strider
Star Strider am 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 Kommentare
Jillian Sweatt
Jillian Sweatt am 28 Jan. 2016
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 am 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?

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 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.

Community Treasure Hunt

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

Start Hunting!

Translated by