suppressing the display of ans

236 visualizaciones (últimos 30 días)
Dale
Dale el 9 de Feb. de 2012
Comentada: Nikhanze el 21 de Dic. de 2023
How can I suppress or omit the display of the "ans" when executing my function?
  8 comentarios
Kavya
Kavya el 24 de Ag. de 2023
When you enter a command without a semicolon at the end, MATLAB displays the result.
>> x = 5 + 1
x =
6
Nikhanze
Nikhanze el 21 de Dic. de 2023
Enter k = 8-2; including the semicolon at the end. The result won't appear in the command window,but you can see the value of k in workspace browser

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 26 de Sept. de 2014
Editada: Stephen23 el 26 de Sept. de 2014
It seems that the semi-colon is not the desired solution. If you can change the function, one solution is to use nargout and simply not define an output for nargout==0:
function x = temp
if nargout>0
x = 2;
end
end
when called in the command window:
>> temp
>> x = temp
x =
2
Some MATLAB functions do this, or have a look at calendar for a similar concept.

Más respuestas (7)

Wayne King
Wayne King el 9 de Feb. de 2012
Put a semicolon at the end of the line.
x = randn(8,1);
fft(x);
  4 comentarios
George Fega
George Fega el 22 de Abr. de 2017
You are the best, dude! Thank you vrey much!
Le Yu
Le Yu el 31 de En. de 2019
That's exactly what I need!! THx

Iniciar sesión para comentar.


Matt Tearle
Matt Tearle el 9 de Feb. de 2012
What Wayne said. But to dig a bit deeper: given that you specifically said "function", I'm guessing you might be confused by the whole local vs global variable thing.
If you have
function y = myfun(x)
y = cos(x.^2);
Then you say
>> z = myfun(pi)
The value of pi is passed into the function where it is assigned to the local variable x; the value of the local variable y is computed and returned to the base workspace according to how the function was called at the command line. In this case, the return value is assigned to the (base) variable z. Because there's no semicolon at the end of that line, the output from that assignment is echoed to the command window. So if you call it as
>> myfun(pi)
MATLAB, as always, assigns the calculated value to ans. Again, there's no semicolon, so you see the result displayed. Note that this display has nothing to do with the line y = cos(x.^2); in the function. If you leave off the semicolon there, you'll see that assignment echoed to the command window -- y = -0.9027 -- and the assignment to ans as well -- ans = -0.9027.
If the function has no return values, nothing will be assigned to ans:
function myfun(x)
plot(x)
>> myfun(1:5)

Allen Bibal
Allen Bibal el 25 de Feb. de 2017
Editada: Allen Bibal el 25 de Feb. de 2017
A=5;
B=4;
z=A+B;
disp(Z)

Pramod Bhat
Pramod Bhat el 9 de Feb. de 2012
It is not possible. Where there are no arguments MATLAB automatically makes "ans" a variable and assigns value to it. You cant hide it.
  1 comentario
Walter Roberson
Walter Roberson el 9 de Feb. de 2012
Probably not correct. You can override the display() function, which is what is invoked to output a value when there is no semi-colon after an expression.

Iniciar sesión para comentar.


Kenneth Cantos
Kenneth Cantos el 25 de Ag. de 2016
is there a code that can hide an output on the command window but the item to be hide will be needed on the succeeding formula.
for example:
i will set A=1+2 B=1+3
Formula:
Z=A+B
Z=9
I want to show only the result of Z.
thanks guys.

Niklas Berg
Niklas Berg el 13 de Oct. de 2022
If your output argument is called x for example just type
clear x
in the end of your function. Then x won't show as ans in the command window.

Kavya
Kavya el 24 de Ag. de 2023
When you enter a command without a semicolon at the end, MATLAB displays the result.
>> x = 5 + 1
x =
6
Optionally, you can add a semicolon to the end of a command so that the result is not displayed. MATLAB still executes the command, and you can see the variable in the Workspace browser.
>> x = 5 + 1;

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by