Variable plot title and legend

4 views (last 30 days)
Agnieszka Polak
Agnieszka Polak on 6 Dec 2019
Edited: Agnieszka Polak on 6 Dec 2019
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.

Answers (2)

Star Strider
Star Strider on 6 Dec 2019
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 on 6 Dec 2019
Edited: KALYAN ACHARJYA on 6 Dec 2019
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);

Community Treasure Hunt

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

Start Hunting!