Matlab is giving me an unwanted ans = output, even with semicolons
Ältere Kommentare anzeigen
I'm new to matlab and I have been tasked with producing a very simple program to calculate mean, standard deviation, and standard error without using the built in functions. The program functions fine and gives me the correct outputs but it also displays the mean as an ans = even when I have semicolons on everything. Here's my code
function [xbar,xsd,xse] = fstats(infile)
%A function that calculates the mean, standard deviation, and standard
%error of a 1D Arry
% xbar = mean
% xsd = standard deviation
% xse = standard error
[x,n] = fread1col(infile);
%Calculate Mean
xbar = sum(x)/n;
%Calculate standard deviation
sdsum = 0;
for i = 1:n
sdsum = sdsum + (x(i) - xbar)^2;
end
xsd = sqrt((sdsum)/(n-1));
%Calculate standard error
xse = xsd/(sqrt(n));
end
8 Kommentare
M
am 26 Feb. 2018
And how do you call this function ?
Nicholas Backhouse
am 26 Feb. 2018
Adam
am 26 Feb. 2018
Step through line by line in the debugger until you find the line that gives the output.
It's tedious, but I have had cases like this before too where I can't find the source of the ans output so I have to step through.
@Nicholas Backhouse: you still have not shown us how you call this function, although you have been asked multiple times. For example, when I write
sin(pi/2)
I am calling the sin function. How are you calling the function fstats ?
Nicholas Backhouse
am 27 Feb. 2018
@Nicholas Backhouse: if you are not calling this function then it will not cause anything to happen. Either
- you have called it somehow (so it is executed) and it causes ans to be displayed (as you claim in your question), or
- you do not call it (in which case MATLAB does not execute it) and so ans is not displayed by anything related to it (this is what you now claim).
It is not possible for both of these to be true simultaneously.
If you get ans being displayed then you are running some code. What code are you running? How are you running that code?
I suspect that John D'Errico is correct, but without the information that you have been asked for and not given us it is hard to give more advice than "check the code where you call this function".
John D'Errico
am 27 Feb. 2018
"You call it by typing fstats(infile) but I’m not calling it yet, I’m still writing it."
But you also state that "The program functions fine and gives me the correct outputs but it also displays the mean as an ans = even when I have semicolons on everything."
That implies you are testing it using the call
fstats(infile)
exactly as I said. So you ARE calling it. And you are not writing it. That code is already written, by you.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Clocks and Timers finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!