Variable plot title and legend

3 次查看(过去 30 天)
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