suppressing output from user defined function

function f=fib(n);
f=ones(n,1);
for i=1:n-2;
f(i+2,1)=f(i+1,1)+f(i,1);
end;
i have this function to calculate fibbonaci sequence, but i get outputs despite having semicolons on everything.
Can anyone tell me why?

 Respuesta aceptada

Jonathan Chin
Jonathan Chin el 25 de Mzo. de 2016

2 votos

When you call your function use a semi colon at the end of the function to suppress the output.
try f=fib(n) and f=fib(n); in the command line to see the results

1 comentario

Michael
Michael el 25 de Mzo. de 2016
Editada: Michael el 25 de Mzo. de 2016
worked. i'm an idiot -_- Thanks man.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 25 de Mzo. de 2016
You have to put a semi-colon in the line that calls fib() to prevent the default output
abc = fib(19)
fib is called and returns an expression that is assigned to the variable, but the default action for assignment is to also display the result of the assignment. You would need
abc = fib(19);
to suppress it.
This is not under the control of the called function, which does not have control by the time MATLAB makes the decision about whether to output or not.

5 comentarios

Michael
Michael el 25 de Mzo. de 2016
thanks.
That's not the case, though. plot has an output (`Line` object) but does not display it by default. Clearly there is some way to suppress the output from the callee. I'm currently trying to do that for some custom plotting functions and I can only find answers like this.
X = 1;
Y = 2;
custom_func(X,Y) % no output: no output shown
Z = custom_func(X,Y); % 1 output, suppressed with semicolon: output not shown
Z = custom_func(X,Y) % 1 output, unsuppressed: output shown
Z = 3
function out = custom_func(x,y)
result = x+y;
if nargout
out = result;
end
end
@Voss Exactly the behavior I'm looking for, thank you!
Voss
Voss el 25 de Feb. de 2024
You're welcome!

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Mzo. de 2016

Comentada:

el 25 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by