How to add multiple legends in app designer ?
4 views (last 30 days)
Show older comments
Hi all
I am aware that MATLAB lacks the option of adding multiple legends
strangely there are some solutions ,and this , that might be feasible in matlab base. but I need to do that in app designer, while I have a list box that reads the files names from a folder and when I hover over each one of them and click on them, it will plot it and based on the hold condition I activate, it plots the next chosen file over the previous.
I went as far as the code you see bellow but it still keeps one single legend name. Is there any hope to resolve this ?
% Value changed function: HoldPlotsCheckBox
function HoldPlotsCheckBoxValueChanged(app, event)
value = app.HoldPlotsCheckBox.Value;
end
% Value changed function: FilesListBox
function FilesListBoxValueChanged(app, event)
names = {};plots=[];
switch app.HoldPlotsCheckBox.Value
case 0
cla(app.UIAxes)
drawnow;
% FocusUIFigure(app.UIFigure)
% app.UIFigure.Visible = 'off';
% app.UIFigure.Visible = 'on';
filename = app.FilesListBox.Value;
data = xlsread(filename);
plot(app.UIAxes, data(:,1), data(:,2));
case 1
hold(app.UIAxes);
drawnow;
% FocusUIFigure(app.UIFigure)
% app.UIFigure.Visible = 'off';
% app.UIFigure.Visible = 'on';
filename = app.FilesListBox.Value;
names{end+1} = filename;
data = xlsread(filename);
pl=plot(app.UIAxes, data(:,1), data(:,2));
plots(end+1)=pl;
legend(app.UIAxes,plots,names);
hold(app.UIAxes);
end
end
% Button pushed function: UpdatefilesButton
function UpdatefilesButtonPushed(app, event)
files = dir('*.xlsx');
app.FilesListBox.Items = {files.name};
end
0 Comments
Accepted Answer
Mohammad Sami
on 27 Mar 2020
Edited: Mohammad Sami
on 27 Mar 2020
Add a display name to each plot. then turn on the legends at the end
% example
ax = axes;
hold(ax,'on');
for i = 1:10
plot(ax,1:10,1:10,'DisplayName',['line' num2str(i)])
end
legend(ax);
Please note that your variables names and plots will not persist after the function finish executing.
If you want to store this then please add property to your app, then you can store the values in this property.
Also the way this code is written, it does not allow multi select on the file list. if you want to do that you may have to edit this code.
function FilesListBoxValueChanged(app, event)
switch app.HoldPlotsCheckBox.Value
case 0
cla(app.UIAxes)
filename = app.FilesListBox.Value;
data = xlsread(filename);
plot(app.UIAxes, data(:,1), data(:,2),'DisplayName',filename);
case 1
hold(app.UIAxes,'on');
filename = app.FilesListBox.Value;
data = xlsread(filename);
% if you want to store plot reference u need to create a app property.
pl=plot(app.UIAxes, data(:,1), data(:,2),'DisplayName',filename);
legend(app.UIAxes);
hold(app.UIAxes,'off');
end
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!