Legend in GUI: Preset string and colors.

I want to create a legend in a GUI figure, that is independent of the data inserted in the graphs. I want it to be stable, as providing information in the GUI, irrespective of whether all the data colors are present in the grahs. I need it because in this GUI I can choose to visualise several different recordings of data, and not all of them share the same properties, but still I want them all present in the figure legend

Answers (2)

legend() entries can only be present for graphics elements that exist.
However, legend entries can be present for graphics elements whose visibility is turned off.
Therefore for any entry for which there would otherwise be no handle to legend() against, create a line handle with the appropriate colors and markers and set the handle Visible property off. You can then include the handle in the legend list.
dpb
dpb on 14 Jan 2016
Edited: dpb on 14 Jan 2016
I "don't do GUIs" but I'd guess to create a set of hidden lines with the desired characteristics and use them for the legend on creation. Well, let's see how that works ---
>> x=0:20;
>> hL=plot(x,randi(20,length(x),5));
>> legend(num2str([1:5]','Line %d'))
>> set(hL,'yData',nan)
Un-huh, that leaves me with a legend showing the properties of the initial lines but there's nothing on the plot itself. Oh, what about a refinement???
>> hL=plot(x,nan(length(x),5));
>> legend(num2str([1:5]','Line %d'))
does the same end result without ever showing any data on the axes. You will, of course, have to just keep these line handles tucked away somewhere that you can access them if you were to want to change the legend properties and to keep them extant else't the legend automagically update to reflect a change. But, you could, for example, use
set(hL(1),'color','k','linestyle',':','marker','*')
to change that particular line's legend's look.
Mayhaps there's something more clever; that's what strikes me as pretty simple(*).
() That is, while you can save the handles from *legend directly, the line and text handles are all mooshed up together in the object_h handle array returned and so it takes retrieving the correct one of them by a lookup process. The simplification here is you don't want the lines to show anyway and so you can simply change their style characteristics at will to change the legend appearance but with the NaN for yData, the lines themselves will never interfere with whatever else it is you want to put on the graph.
Oh, thought -- you will, however, have to use hold on to prevent a new plot or line command from replacing these lines, thus losing the legend.
Or, with a little more effort you could modify the xData and yData arrays by augmenting the first N columns for the legend with those of the real data. This would, of course, require fixing up the size of the first four to be consonant with those added which may be a hassle, depending.

Asked:

on 14 Jan 2016

Edited:

dpb
on 14 Jan 2016

Community Treasure Hunt

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

Start Hunting!