How can a put a variable into a text string

477 views (last 30 days)
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 Comment
Antonia Ciocoiu
Antonia Ciocoiu on 6 Jul 2018
Edited: Antonia Ciocoiu on 6 Jul 2018
a=(['Testing',num2str(2)]);
plot(x,y) %% plot your variables
xlabel(a)

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Oct 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 Comments
Diana Tsvetkova
Diana Tsvetkova on 14 Mar 2022
it works, and it helped me a lot. Thanks !
Steven Lord
Steven Lord on 14 Mar 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")

Sign in to comment.

More Answers (0)

Categories

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

Tags

Products

Community Treasure Hunt

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

Start Hunting!