Plot legend upon checkbox activation in GUI

5 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2017-8-19
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.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by