MATLAB: Legend causes thousands of function calls

legendperformance

I have a simple matlab script wich loads a log file (MAT-file) of roughly 270Mb and then plots the data in some plots. It takes a while to process the data but after roughly 2min its done. However, if I want to add a legend to the plots the script takes over 90min to execute. Using 'profile' I see that the script causes over 1 million function calls to
scribe/private/get_legendable_children
scribe/private/islegendable
ismethod
cell.strmatch
strmacht
Does anybody know this problem? I don't do any fancy stuff. I only load and plot a Mat file.
Thank you for any ideas!

Best Answer

How are you calling legend? Are you just passing in the legend strings? If so then MATLAB needs to visit every object (such as lines) that are legend'able in order to determine whether they have custom legends, need to have a legend entry, or so on.
If that is what you are doing, then when you call legend(), pass in the explicit list of object handles that you are generating legends for, so that it does not need to visit the other objects.