简体   繁体   中英

Why is MATLAB's legend function so slow, and how to optimize?

Problem

In a GUI I've written I've realized that the largest bottleneck in my code performance is creating/updating the legend.

Currently I delete and recreate the legend at each update of the GUI as the user needs to be able to adjust what is in the legend. I'm currently toggling what is in the legend by adjusting the setting of LINEHANDLE.Annotation.LegendInformation.IconDisplayStyle and updating the legend using legend('off');legend('show');

Testing

The following code snippet shows that the call of legend mostly is limited by the call to legend>make_legend and is fairly independent of the legend content.

close all

n=50; % number of plot lines
S=rand(n); % data
legs=cellstr(char(26*rand(n,10)+97)); % legend entries

profile on %start profiler
plot(S)
legend(legs{:})
profile viewer % view call stats

Question

Is there a better way of updating legend content without deleting it and therefore forcing it to re-call make_legend at recreation?

Furthermore I wonder if it is known why legend in general is so slow and has such odd behavior.

Purpose

I'm adding some information here to avoid the XY Problem . A minimal example of what I'm trying to do is:

I'm building a GUI which plots four lines, let's call them data1 , data2 , linear model 1 , and linear model 2 . The data lines are independent in both color and content, while the linear models both have the same appearance and are connected to respective data line.

I want there to be a legend which only has three entries: data1 , data2 , and linear model . So far, no problem.

I also want there to be three toggle-buttons which toggle the visibility of the four lines on the axes and legend. The buttons are:

  • data1 , which toggles the visibility of both the data1 , and linear model 1 data lines.
  • data2 , which toggles the visibility of both the data2 , and linear model 2 data lines.
  • linear model , which toggles the visibility of the linear model 1 , and linear model 2 data lines.

Attempted Solutions

My first approach was to first only pass three handles to legend and then have the button callbacks adjust the visibility property of the line objects according to above.

This creates the issue that when disabling the first data line and respective linear model the legend entry for linear model also blanks out as it is connected to that specific line object, even though the other is still visible.

My current working approach instead manually sets the DisplayName property of all lines and then the button callbacks adjust each lines Annotation.LegendInformation.IconDisplayStyle property. According to the documentation the user then needs to call legend to force an update.

But this is not implemented, looking at the code of legend.m it is clear that this option only returns the current legend object without any other manipulation. Therefore I'm forced to call legend('off');legend('show'); which triggers a (slow) creation of a new legend object.

This currently works but using profile I can see that the legend creation is half my computation time and has a fairly large effect on user experience when using the GUI on a slower laptop. I've already ensured that my code runs legend('off');legend('show'); only if it really has to.

The question is if any user here is able to call the unreadable, yet accessible class methods of matlab.graphics.illustration.Legend to trigger an update of an existing object without forcing it to delete and recreate. Thereby doing what the MATHWORKS documentation claims to be implemented (although it is not) in legend .

Alternatively I'm open to finding a different way of changing which line objects the current legend is tracking efficiently.

You can try to change the properties of the legend object directly. Look at http://www.mathworks.com/help/matlab/ref/legend-properties.html for a list of properties. The legend object can be accessed by:

% Assign the output of legend to a variable. Do this only once at creation.
l = legend(<some arguments>); 

% Example: change the location of the legend to 'north'
l.Location = 'north'; 

I think this is what you asked for, but I'm not sure about any efficiency gains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM