Get rid of unwanted output
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Krish Desai
el 5 de Dic. de 2015
I have the following code:
function output=beautyofmath(i)
for i = 1:9
if i == 1
j(i, 1) = i;
else
j(i, 1) = j(i - 1, 1) * 10 + i;
end
j(i, 2) = i;
j(i, 3) = j(i, 1) * 8 + j(i, 2);
output=fprintf('%d x 8 + %d = %d\n', j(i, 1), j(i, 2), j(i, 3));
end
and it outputs
1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321
ans =
30
I don't want the ans=30 part, does anybody know why it's showing up and how to get rid of it?
0 comentarios
Respuesta aceptada
Stephen23
el 5 de Dic. de 2015
Editada: Stephen23
el 5 de Dic. de 2015
That 30 is the output of fprintf, exactly as you have coded it. The fprintf documentation clearly describes its output as " nbytes = fprintf(__) returns the number of bytes that fprintf writes". If you don't want the output of fprintf (i.e. the number of bytes), then don't use it:
function beautyofmath(i)
...
fprintf('%d x 8 + %d = %d\n', j(i, 1), j(i, 2), j(i, 3));
end
However it seems you are confused about the difference between a function output and the text that fprintf is printing in your command window. You function currently outputs the value 30 (the bytes value from fprintf), and prints those lines to your command window. So although you write that "and it outputs" those lines of text, it actually prints those lines and outputs the value 30.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!