How can a put a variable into a text string

491 Ansichten (letzte 30 Tage)
Mitchell Frechette
Mitchell Frechette am 28 Okt. 2016
Kommentiert: Steven Lord am 14 Mär. 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 Kommentar
Antonia Ciocoiu
Antonia Ciocoiu am 6 Jul. 2018
Bearbeitet: Antonia Ciocoiu am 6 Jul. 2018
a=(['Testing',num2str(2)]);
plot(x,y) %% plot your variables
xlabel(a)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 28 Okt. 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 Kommentare
Diana Tsvetkova
Diana Tsvetkova am 14 Mär. 2022
it works, and it helped me a lot. Thanks !
Steven Lord
Steven Lord am 14 Mär. 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")

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by