How can a put a variable into a text string

823 visualizaciones (últimos 30 días)
Mitchell Frechette
Mitchell Frechette el 28 de Oct. de 2016
Comentada: Steven Lord el 14 de Mzo. de 2022
I want to put a variable onto a graph.
I have tried text(x,y,'Text ' num2str(variable) ' moretext') with no luck.
The only information I can find is how to put a variable in the title of a graph. I simply want it to be displayed at a location of my choosing on the graph.
  1 comentario
Antonia Ciocoiu
Antonia Ciocoiu el 6 de Jul. de 2018
Editada: Antonia Ciocoiu el 6 de Jul. de 2018
a=(['Testing',num2str(2)]);
plot(x,y) %% plot your variables
xlabel(a)

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 28 de Oct. de 2016
Use the sprintf function:
text(x, y, sprintf('Text %f more text', variable))
NOTE This is UNTESTED CODE but it should work. Change the format descriptor in sprintf as necessary to provide the result you want.
  2 comentarios
Diana Tsvetkova
Diana Tsvetkova el 14 de Mzo. de 2022
it works, and it helped me a lot. Thanks !
Steven Lord
Steven Lord el 14 de Mzo. de 2022
If you're using a recent release (one that supports string arrays) you can use that.
text(0.5, 0.5, "Text " + pi + " more text")

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by