MATLAB Answers

Input a variable into a plot title

조회 수: 5,813(최근 30일)
Luke 2014년 9월 10일
댓글: Alan Keenan 2020년 5월 21일
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)
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.
  댓글 수: 3
Alan Keenan
Alan Keenan 2020년 5월 21일
The answer given by Soumitra Vadnerkar works well for me.

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

채택된 답변

Steven Lord
Steven Lord 2019년 5월 22일
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:
f = 70;
c = (f-32)/1.8;
title("Temperature is " + c + " C")

추가 답변(2개)

Adam 2014년 9월 10일
title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?
  댓글 수: 8
Adam 2017년 2월 14일
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!

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

MathWorks Support Team
MathWorks Support Team 2019년 5월 22일
How about this, given in help documentation:
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])
  댓글 수: 1
MathWorks Support Team
MathWorks Support Team 2019년 5월 22일
Thanks Soumitra Vadnerkar for this answer.

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

Community Treasure Hunt

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

Start Hunting!

Translated by