Legends are parented by the figure, not the axes, so you get a list of figure children like this:
>> get( gcf, 'Children' )
ans =
6×1 graphics array:
Legend (a, b)
Axes
Legend (a, b)
Axes
Legend (a, b)
Axes
so I guess if you have the axes handle you may be able to search the figure's child list for the index of that axes, then take the legend handle previous to it (The child objects seem to be stored in reverse order and legends are apparently stored after their respective axes even if not added in that order - I tested creating all the axes and plots first, then all the legends and it still gave the above order)
As ever with having to fish out handles to graphics objects after the fact it is hacky and potentially brittle though.
So, like the joke about an Irishman in a small town being asked how to get to Dublin, answering 'Well, if I were you I wouldn't start from here!', if at all possible I would always keep hold of all my handles when I create the plots if I'm going to need them later on.
I would also give an explicit axes handle to the legend and plotting calls.
Best Answer