Variable plot title and legend

조회 수: 4 (최근 30일)
Agnieszka Polak
Agnieszka Polak 2019년 12월 6일
편집: Agnieszka Polak 2019년 12월 6일
Hello!
I am writing a code in which the user can input their own functions and minimum and maximum values. I'd like the title and legend to change according to the function input by the user, so instead of "Function 1" it could say sin(x) or whatever else the used chooses. I don't know how to do this. Any ideas? Help would be much appreciated.

답변 (2개)

Star Strider
Star Strider 2019년 12월 6일
The easiest way is to use the sprintf function inside the title and legend, respectively. It also allows straightforward inclusion of numerical and other string data. Remember to use double backslants (\\) if you want to include a backslant (for example if you wnat to use TeX or LaTeX strings) so they will be interpreted coorrectly and not as sprintf control characters.

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 6일
편집: KALYAN ACHARJYA 2019년 12월 6일
Other way apart from @Star's Answered: For variable data title plot, see this example:
xmin=input('please input your minimum value: ');
xmax=input('please input your maximum value: ');
data=xmin:0.01:xmax;
plot(data);
hold on;
title("Plot with xmin: " + xmin);
xmin=input('please input your minimum value: ');
xmax=input('please input your maximum value: ');
data=xmin:0.01:xmax;
plot(data);
title("Plot with xmin: " + xmin);

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by