fprintf command output and extra output

2 ビュー (過去 30 日間)
Jack Crespo
Jack Crespo 2019 年 4 月 22 日
コメント済み: Jack Crespo 2019 年 4 月 23 日
I am trying to change a sound file to ouput an echo.I have to output a column vector of size [4 1]. My output is:
output=echo_gen([-.5;0;.5;0],1,0,.5)
output =
-0.7500
0
0.7500
0
However, this is incorrect. I think it needs to be outputing like this:
output=echo_gen([-.5;0;.5;0],1,0,.5)
output =
-0.75
0
0.75
0
I tried using the fprintf('%g',output) and fprintf('%.2f',output) to remove the trailing zeros, but when i do that, i get this:
output=echo_gen([-.5;0;.5;0],1,0,.5)
-0.75
0
0.75
0
output =
15
Where is the output=15 coming from, and how do i remove it to get only the colum vector?
Thanks in advance.

採用された回答

Stephen23
Stephen23 2019 年 4 月 22 日
編集済み: Stephen23 2019 年 4 月 22 日
You used the wrong operator. If you want to print to string, use sprintf.
"Where is the output=15 coming from..."
The fprintf documentation clearly describes that its only output is "Number of bytes that fprintf writes, returned as a scalar", which in your case also corresponds to characters on each line: 5 + 1 + 4 + 1 + 4(newlines) = 15.
"...and how do i remove it to get only the colum vector?"
Just suppress the function output using a semi-colon:
output=echo_gen(...);
^ you need this!
But as mentioned at the start of my answer, I suspect that you really want to be using sprintf, not fprintf, as you indicate that you want to print text to an output argument.
  1 件のコメント
Jack Crespo
Jack Crespo 2019 年 4 月 23 日
Thanks Stephen! Im a junior in high school, and i am taking an online course, and this was one of the assignments. I actually was doing part of the problem incorrectly, and i needed to add some more logic in my code to get it working. But your explanation of fprintf and sprintf cleared up a lot of confusion I had, so thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by