Using text, 2 variables, and decimals in fprintf

fprintf(["The value you entered "+'%.1f°C\n',temp + "is equal to "+'%.1f°F\n',ans])
Can someone help me make this line work. I feel like I'm close but I keep getting errors.

 採用された回答

Star Strider
Star Strider 2020 年 9 月 26 日
編集済み: Star Strider 2020 年 9 月 26 日

0 投票

Try this:
fprintf("The value you entered %.1f°C\nis equal to %.1f°F\n", temp, ans)
so:
temp = 10;
ans = 50;
fprintf("The value you entered %.1f°C\nis equal to %.1f°F\n", temp, ans)
produces:
The value you entered 10.0°C
is equal to 50.0°F
To get it all on one line, replace the first ‘\n’ with a space:
fprintf("The value you entered %.1f°C is equal to %.1f°F\n", temp, ans)
producing:
The value you entered 10.0°C is equal to 50.0°F
.

2 件のコメント

Breanna Sokolik
Breanna Sokolik 2020 年 9 月 26 日
Thank you so much!!!
Star Strider
Star Strider 2020 年 9 月 26 日
As always, my pleasure!

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

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 9 月 26 日

0 投票

The documentation page is a great place to start. There, you'll see that the variables have to come at the end. Also, you don't need square brackets.
fprintf("The value you entered "+'%.1f°C\n' + "is equal to "+'%.1f°F\n',10,20)
The value you entered 10.0°C
is equal to 20.0°F

2 件のコメント

Breanna Sokolik
Breanna Sokolik 2020 年 9 月 26 日
Is there anyway to get it all on one line?
Cris LaPierre
Cris LaPierre 2020 年 9 月 26 日
Yes. Remove the first \n

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

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by