MATLAB Answers

Plot legend upon checkbox activation in GUI

조회 수: 9(최근 30일)
JB
JB 19 Aug 2017
답변: Walter Roberson 19 Aug 2017
I am new to matlab gui and working on my first program. I have ~10 datasets that I need to plot , but only when activated by a checkbox selection where two plots will be generated in axes1 and axes2. Sometimes I only want to activate some in random, but I need a lengend on those activateed. And I need the legend to disappear again upon unclicking the checkbox. I have made to plots behave as I want but I can't figure out how to handle the legend. The same legend (like Temperature) should appear in axes1 and axes2 when a checkbox is activated. Here is my code so far:
function checkbox1_Callback(hObject, eventdata, handles)
if get(handles.checkbox1,'Value')
hold( handles.axes1, 'on' )
handles.plotCDS1 = plot(dataS1(:,1),NormCdS1(:,1),'LineWidth',2,'Color', [0 0 0],'parent',handles.axes1);
hold( handles.axes2, 'on' )
handles.plotHTS1 = plot(dataS1(:,1),dataS1(:,3),'LineWidth',2,'Color', [0 0 0],'parent',handles.axes2);
guidata(hObject,handles); % do this to save the updated handles structure
else
if ~isempty(handles.plotCDS1);
delete(handles.plotCDS1);
~isempty(handles.plotHTS1);
delete(handles.plotHTS1);
end
end
and the second checkbox:
function checkbox2_Callback(hObject, eventdata, handles)
if get(handles.checkbox2,'Value')
hold( handles.axes1, 'on' )
handles.plotCDS2 = plot(dataS2(:,1),NormCdS2(:,1),'LineWidth',2,'Color', [1 0 0],'parent',handles.axes1);
hold( handles.axes2, 'on' )
handles.plotHTS2 = plot(dataS2(:,1),dataS2(:,3),'LineWidth',2,'Color', [1 0 0],'parent',handles.axes2);
guidata(hObject,handles); % do this to save the updated handles structure
else
if ~isempty(handles.plotCDS2);
delete(handles.plotCDS2);
~isempty(handles.plotHTS2);
delete(handles.plotHTS2);
end
end
And then I have 8 checkboxes more. Please help... THANKS

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(1개)

Walter Roberson
Walter Roberson 19 Aug 2017
The easiest way of handling this is to plot everything with appropriate legend, and then to set the 'Visible' property of the appropriate line 'on' or 'off'. With recent MATLAB, you might want to add a
legend update
call.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by