Get rid of unwanted output

조회 수: 4 (최근 30일)
Krish Desai
Krish Desai 2015년 12월 5일
편집: Stephen23 2015년 12월 5일
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에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by