MATLAB: Get lineseries handle


I'm trying to use set to set the display name of each legend label.
If I have:
Then try to do:
set(h(1),'DisplayName','data label')
It's not working. Do I have to set the legend to be displayed first or does this automatically display it then change the label of each data series?

Best Answer

  • Setting the DisplayName property does not automatically show the legend. You still need to explicitly show the legend:
    h = plot( 1:5, rand(1,5), 6:10, rand(1,5) );
    set(h(1), 'DisplayName', 'some data');
    set(h(2), 'DisplayName', 'more data');
    legend show