legend causes thousands of function calls

2 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 18 Oct 2013
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.
  2 Comments
xlr8t
xlr8t on 19 Oct 2013
Hi Walter,
thank you for your answer. Indeed, I was just passing strings to legend(). I now changed it from
plot(t,y1);
hold on;
plot(t,y2)
legend('entry1','entry2');
to
p1 = plot(t,y1);
hold on;
p2 = plot(t,y2)
legend([p1;p2],'entry1','entry2');
It really seems that this was the key problem. The execution time is now down to 6min. Now it seems, that the legend coloring takes a big portion of the execution time. Don't know if that can be improved as well.
Thanks
Jos (10584)
Jos (10584) on 19 Oct 2013
Using
legend(handles, CellArrayofStrings)
might be faster than
legend(handles, String1, String2, ...)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!