How can a put a variable into a text string

조회 수: 518 (최근 30일)
Mitchell Frechette
Mitchell Frechette 2016년 10월 28일
댓글: Steven Lord 2022년 3월 14일
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
Antonia Ciocoiu
Antonia Ciocoiu 2018년 7월 6일
편집: Antonia Ciocoiu 2018년 7월 6일
a=(['Testing',num2str(2)]);
plot(x,y) %% plot your variables
xlabel(a)

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2016년 10월 28일
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
Diana Tsvetkova
Diana Tsvetkova 2022년 3월 14일
it works, and it helped me a lot. Thanks !
Steven Lord
Steven Lord 2022년 3월 14일
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")

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by