Get rid of unwanted output
7 ビュー (過去 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?
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File 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!