Clear Filters
Clear Filters

Legend Button displays data1, data2, etc

20 views (last 30 days)
Scott
Scott on 9 Jan 2014
Commented: Steven Lord on 5 May 2017
Hi,
I've been searching for a while and I'm hoping someone can help me figure this out.
How can I make the legend button in the figure toolbar display the data by name instead of 'data1,data2,data3,etc'?
I've set the 'Tag' property for each lineseries to what I want displayed in the legend.
Is there a way to change the callback function for icons such as the legend button in the default toolbar?
Thanks so much,
Scott

Answers (1)

KAE
KAE on 4 May 2017
You need to include strings in the legend command.
% Plot some data
figure;
h1 = plot(rand(1,7), rand(1,7));
hold on;
h2 = plot(rand(1,7), rand(1,7));
h3 = plot(rand(1,7), rand(1,7));
% Label each line in the legend
legend([h1 h2 h3], 'First line', 'Second line', 'Third line');
  1 Comment
Steven Lord
Steven Lord on 5 May 2017
That's one way. Another, as stated in the legend function documentation, is:
"legend('show') creates a legend for the current axes or chart returned by gca. The legend automatically updates when you add or delete graphics objects from the axes. For the labels, the legend uses the text from the DisplayName properties of the graphics objects. If the DisplayName property is empty, then the legend uses a label of the form 'dataN'."
So you could assign a value to the DisplayName property of the graphics object when you construct it (or afterwards) and legend will use that value.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!