Input a variable into a plot title
Mostra commenti meno recenti
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 Commenti
Star Strider
il 10 Set 2014
Your code works perfectly for me (R2014a) with the string you posted. I can’t reproduce the error you report.
Soumitra Vadnerkar
il 26 Set 2017
Modificato: Walter Roberson
il 26 Set 2017
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
il 21 Mag 2020
The answer given by Soumitra Vadnerkar works well for me.
Risposta accettata
Più risposte (2)
MathWorks Support Team
il 22 Mag 2019
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 Commenti
MathWorks Support Team
il 22 Mag 2019
Giuseppe Degan Di Dieco
il 21 Ott 2021
Thank you for highlighting the documentation's content to beginners!
Best.
Adam
il 10 Set 2014
title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?
8 Commenti
Peter
il 5 Gen 2015
If i try to make that for two variable they appear together
Some idea to solve that?
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
il 5 Gen 2015
Okey, i had a syntaxis problem with that. Sorry.
Adam
il 5 Gen 2015
title( ['just an example of ' num2str( variable ) ', ' num2str( otherVariable ) ' that isnt working'] )
Image Analyst
il 5 Gen 2015
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
il 14 Feb 2017
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
il 14 Feb 2017
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
il 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!
Categorie
Scopri di più su Title in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!