Matlab is giving me an unwanted ans = output, even with semicolons
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nicholas Backhouse
el 26 de Feb. de 2018
Editada: John D'Errico
el 1 de Mzo. de 2018
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 comentarios
Stephen23
el 27 de Feb. de 2018
Editada: Stephen23
el 28 de Feb. de 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
el 27 de Feb. de 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.
Respuesta aceptada
John D'Errico
el 26 de Feb. de 2018
Editada: John D'Errico
el 26 de Feb. de 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 comentarios
John D'Errico
el 1 de Mzo. de 2018
Editada: John D'Errico
el 1 de Mzo. de 2018
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!