I want to hold legends from different callback on same plot.

3 views (last 30 days)
I have GUI with two PUSHBUTTONs to import the seperate excel files and numbers of CHECKBOX to plot the respective graphs on * same figure* to compare the results. When i click one checkbox it plot the graph with its legend. When i click other checkbox it also plot the graph on same figure but previous legend vanishes and it overrides new legend. I want to keep old legend also to compare the graphs. Please suggest...
if true
function AFR_Test_Callback(hObject, eventdata, handles)
% hObject handle to AFR_Test (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of AFR_Test
handles = guidata(hObject);
a2 = handles.a;
AFR2 = handles.AFR;
LGD2 = handles.LGD1
if get(handles.AFR_Test,'Value')
figure(1)
hold on
handles.h1 = plot(a2,AFR2,'*--')
handles.rt = legend(LGD2,'Location','southwest','FontSize',8,'FontWeight','bold')
title('AFR')
xlabel('Speed (RPM)','FontSize',8,'FontWeight','bold');
ylabel('AFR','FontSize',8,'FontWeight','bold');
grid on;
hold on;
guidata(hObject,handles);
else
if ~isempty(handles.h1)
set(handles.h1,'Visible','off');
delete (handles.rt);
end
end
guidata(hObject,handles);
end
function AFR_Sim_Callback(hObject, eventdata, handles)
% hObject handle to AFR_Sim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of AFR_Sim
handles = guidata(hObject);
a2 = handles.a;
AFR_2 = handles.AFR_1;
TEST2 = handles.TEST1
if get(handles.AFR_Sim,'Value')
figure(1)
hold on
handles.h1 = plot(a2,AFR2,'*--')
handles.rt = legend(TEST2,'Location','southwest','FontSize',8,'FontWeight','bold')
title('AFR')
xlabel('Speed (RPM)','FontSize',8,'FontWeight','bold');
ylabel('AFR','FontSize',8,'FontWeight','bold');
grid on;
hold on;
guidata(hObject,handles);
else
if ~isempty(handles.h1)
set(handles.h1,'Visible','off');
delete (handles.rt);
end
end

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 24 Aug 2016
It seems that you might get what you want with the legendflex file exchange submission - it allows multiple legends to be added to an axis, seems like what you want to do. It might require some additional adaptations of your code but you'll figure it out.
  1 Comment
Sandy Baha
Sandy Baha on 24 Aug 2016
Appreciate to your quick answer. I have used legendflex but it still overriding the legends.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!