Get rid of unwanted output

5 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2015-12-5
编辑:Stephen23 2015-12-5
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 个)

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by