Matlab is giving me an unwanted ans = output, even with semicolons

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

Stephen23
Stephen23 am 26 Feb. 2018
Bearbeitet: Stephen23 am 26 Feb. 2018
@Nicholas Backhouse: How do you call this function? What is fread1col? Have you checked its code too?
And how do you call this function ?
Fread1col just reads the first column of infile. It outputs as array x and the length of the array n, and I’ve checked there too.
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 ?
You call it by typing fstats(infile) but I’m not calling it yet, I’m still writing it.
Stephen23
Stephen23 am 27 Feb. 2018
Bearbeitet: Stephen23 am 28 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".
"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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

John D'Errico
John D'Errico am 26 Feb. 2018
Bearbeitet: John D'Errico am 26 Feb. 2018
You have not answered HOW you call the function. That is actually quite important.
For example, if you call it like this:
fstats(infile)
then you will still get stuff on the command line as ans. In fact, it will return xbar (the mean) as ans. So it is VERY probable that is what you are doing. But how can we know, since you refuse to provide the necessary information?
And as others have said, we have not seen the code for fread1col. I know, you checked it over. But that does not convince me, or any of us.
Next, it is entirely possible that you have several copies of these functions on your search path. An old copy might still be missing the semi-colons. So do these things:
which fstats -all
which fread1col -all

4 Kommentare

function [x,n] = fread1col(infile);
% A function that reads the first column of a file
data = load(infile);
x = data(:,1);
n = length(x);
end
This is fread1col
As I suggested, you are most likely calling it as
fstats(infile)
The result will be the first returned argument is dumped into the variable ans. In turn, since there is no semicolon as I wrote it there, that is then dumped to the command line. Since this is exactly what you claim is happening, that is my guess as to what you did. But I cannot know, since we have repeatedly asked you HOW you called fstats and you have never told us!
I was calling it by clicking run, which I now see has the same effect as typing fstats(infile) without a ;
If I physically type it into the command window with a ; there is no unwanted output, thanks.
If you click run on a function, then you ARE calling it essentially as I have said.
When you try to "run" a function, it calls it at the command line, with NO input arguments. But it still returns output arguments. So you are implicitly typing
fstats
at the command line. This is what I have said many times now.
So what happens when you try to use a function as above? If the function returns an output argument, then it returns the very FIRST argument only.
But if you have supplied no place to put that output argument into, it stuffs it into the default variable ans.
So as I have said many times, you ARE using the function. Just because all you did was click on run, that still uses it, as I have described above.
If you type it into the command line, did you put a semi-colon at the end? So
fstats;
or
fstats(infile);
While either of those forms will dump the results into ans, MATLAB will not display the variable ans. For example, the same applies to ANY function in MATLAB..
mean(1:5)
ans =
3
So when called with no semi-colon, and no output variable, the result gets dumped into ans.
clear ans
So ans no longer exists.
mean(1:7);
So if a semi-colon is used, ans still gets created. But nothing was dumped on the command window.
ans
ans =
4
As you can see, ans was resurrected. It now has the value 4 because I changed the input, so you can see that it is not the same ans as before.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by