Input a variable into a plot title

2.790 visualizaciones (últimos 30 días)
Luke
Luke el 10 de Sept. de 2014
Editada: MathWorks Support Team el 22 de Nov. de 2024 a las 4:00
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 comentarios
Soumitra Vadnerkar
Soumitra Vadnerkar el 26 de Sept. de 2017
Editada: Walter Roberson el 26 de Sept. de 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
Alan Keenan el 21 de Mayo de 2020
The answer given by Soumitra Vadnerkar works well for me.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 22 de Nov. de 2024 a las 0:00
Editada: MathWorks Support Team el 22 de Nov. de 2024 a las 4:00
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")

Más respuestas (2)

MathWorks Support Team
MathWorks Support Team el 22 de Mayo de 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 comentarios
MathWorks Support Team
MathWorks Support Team el 22 de Mayo de 2019
Thanks Soumitra Vadnerkar for this answer.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco el 21 de Oct. de 2021
Thank you for highlighting the documentation's content to beginners!
Best.

Iniciar sesión para comentar.


Adam
Adam el 10 de Sept. de 2014
title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?
  8 comentarios
Walter Roberson
Walter Roberson el 14 de Feb. de 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
Adam el 14 de Feb. de 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!

Iniciar sesión para comentar.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by