Input a variable into a plot title

4,751 views (last 30 days)
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)
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.
Alan Keenan
Alan Keenan on 21 May 2020
The answer given by Soumitra Vadnerkar works well for me.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 22 May 2019
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:
f = 70;
c = (f-32)/1.8;
title("Temperature is " + c + " C")

More Answers (2)

MathWorks Support Team
MathWorks Support Team on 22 May 2019
How about this, given in help documentation:
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco on 21 Oct 2021
Thank you for highlighting the documentation's content to beginners!

Sign in to comment.

Adam on 10 Sep 2014
title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?
Adam on 14 Feb 2017
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!

Sign in to comment.


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!