MATLAB: How to create a legend with multiple outputs

appdesignerlegendMATLABoutputwith

Is it possible to use legend with multiple outputs in App Designer and UI Axes ?

Best Answer

The recommended workflow for creating a legend entry that does not match an existing plot in your data is to create a separate entry in your plot with either empty or NaN data. This will render the new entry invisible, but it will still show up in the legend. Then you can adjust the properties of that new object to make the legend entry appear as you require.
Below is the example on how you can do it. Please run the below code in command line and also in App Designer.
hFig = figure;
hAx = axes(hFig);
hScatter= scatter(hAx,1:10,1:10,'b');
hold on;
hObj(1)=plot(NaN,'b','Marker','o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','b','LineStyle','none','MarkerSize',10);
hObj(2)=plot(NaN,'b','Marker','o','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','b','LineStyle','none','MarkerSize',12);
lg=legend(hObj,{'My Data 1','My Data 2'});