Input a variable into a plot title
조회 수: 2,833 (최근 30일)
이전 댓글 표시
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) title(str) 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. Cheers!
댓글 수: 3
Soumitra Vadnerkar
2017년 9월 26일
편집: Walter Roberson
2017년 9월 26일
How about this, given in help documentation:
figure
plot((1:10).^2)
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])
채택된 답변
Steven Lord
2024년 11월 22일 0:00
편집: MathWorks Support Team
2024년 11월 22일 4:00
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: plot((1:10).^2) f = 70; c = (f-32)/1.8; title("Temperature is " + c + " C")
추가 답변 (2개)
MathWorks Support Team
2019년 5월 22일
How about this, given in help documentation:
plot((1:10).^2)
f = 70;
c = (f-32)/1.8;
title(['Temperature is ',num2str(c),' C'])
댓글 수: 2
Giuseppe Degan Di Dieco
2021년 10월 21일
Thank you for highlighting the documentation's content to beginners!
Best.
Adam
2014년 9월 10일
title( ['just an example of ' num2str( variable ) ' that isnt working'] )
Does that work?
댓글 수: 8
Walter Roberson
2017년 2월 14일
sprintf() would work fine in R2013* . However, the example given
str = sprintf('just an example of %d that isnt working', variable)
would be best for the case where the variable is an integer. If you have a variable that is numeric but not an integer, you would use a %e or %f or %g format.
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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!