Input a variable into a plot title

I've trawled through these forums and found loads on inputting variables as titles, however, none of them work for me. Im trying to change the title of a plot with respect to a parameter the user will enter in the function. First i call a string for example str = sprintf('just an example of %d that isnt working', variable) title(str) However only the 'just an example of' is printed out on the title of the plot, everything after and including the variable has dissapeared, this happens when i move the variable about in the title too. Cheers!

3 件のコメント

Star Strider
Star Strider 2014 年 9 月 10 日
Your code works perfectly for me (R2014a) with the string you posted. I can’t reproduce the error you report.
Soumitra Vadnerkar
Soumitra Vadnerkar 2017 年 9 月 26 日
編集済み: Walter Roberson 2017 年 9 月 26 日
How about this, given in help documentation:
figure
plot((1:10).^2)
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])
Alan Keenan
Alan Keenan 2020 年 5 月 21 日
The answer given by Soumitra Vadnerkar works well for me.

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

 採用された回答

Steven Lord
Steven Lord 2024 年 11 月 22 日
編集済み: MathWorks Support Team 2024 年 11 月 22 日

26 投票

With the introduction of the string array in release R2016b (and the ability to enclose text data in double quotes to create a string in release R2017a) you can also use: plot((1:10).^2) f = 70; c = (f-32)/1.8; title("Temperature is " + c + " C")

1 件のコメント

Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 10 月 21 日
Brilliant and easy, thanks!
Best.

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

その他の回答 (2 件)

MathWorks Support Team
MathWorks Support Team 2019 年 5 月 22 日

6 投票

How about this, given in help documentation:
plot((1:10).^2)
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])

2 件のコメント

MathWorks Support Team
MathWorks Support Team 2019 年 5 月 22 日
Thanks Soumitra Vadnerkar for this answer.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 10 月 21 日
Thank you for highlighting the documentation's content to beginners!
Best.

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

Adam
Adam 2014 年 9 月 10 日

4 投票

title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?

8 件のコメント

Peter
Peter 2015 年 1 月 5 日
If i try to make that for two variable they appear together
Some idea to solve that?
Adam
Adam 2015 年 1 月 5 日
編集済み: Adam 2015 年 1 月 5 日
Two variables in what way?
You can concatenate as many substrings as you want in this way so you don't have to include all variables in the one middle part, you can add an e.g.
', '
between two variables.
Peter
Peter 2015 年 1 月 5 日
Okey, i had a syntaxis problem with that. Sorry.
Adam
Adam 2015 年 1 月 5 日
title( ['just an example of ' num2str( variable ) ', ' num2str( otherVariable ) ' that isnt working'] )
Image Analyst
Image Analyst 2015 年 1 月 5 日
Peter, then use the sprintf() version. In my opinion, I think it's easier and more straightforward than using num2str and all those quotes.
Walter Roberson
Walter Roberson 2017 年 2 月 14 日
Dayan Guerra comments to Adam
Thank you, this is the only way I can it work, the sprintf() version is not working at all on mt2013
Walter Roberson
Walter Roberson 2017 年 2 月 14 日
sprintf() would work fine in R2013* . However, the example given
str = sprintf('just an example of %d that isnt working', variable)
would be best for the case where the variable is an integer. If you have a variable that is numeric but not an integer, you would use a %e or %f or %g format.
Adam
Adam 2017 年 2 月 14 日
sprintf is definitely a neater approach. Back in 2014 I was less familiar with using sprintf so it didn't spring to mind for this!

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

カテゴリ

タグ

タグが未入力です。

質問済み:

2014 年 9 月 10 日

編集済み:

2024 年 11 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by