在 GUI 中激活复选框时绘制图例



我是 matlab gui 的新手,正在开发我的第一个程序。我需要绘制 ~10 个数据集,但只有在由复选框选择激活时,其中将在轴 1 和轴 2 中生成两个图。有时我只想随机激活一些,但我需要对激活的那些进行长时间激活。我需要在取消单击复选框后再次消失图例。我已经让情节按照我想要的方式行事,但我不知道如何处理这个传说。激活复选框时,轴 1 和轴 2 中应显示相同的图例(如温度(。 这是我到目前为止的代码:

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

和第二个复选框:

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 

然后我还有 8 个复选框。请帮忙...谢谢

每个轴都需要单独的图例。 您可以在legend调用中指定轴。 以下是它在 checkbox1 回调函数中的外观:

...
if get(handles.checkbox1,'Value')
% your plot code for axes 1  and 2
...
legend(handles.axes1,'Location','northeast'); % modify according to desired labels and such.
legend(handles.axes2,'Location','northeast');
else
% turn things off like before
legend(handles.axes1,'off'); 
legend(handles.axes2,'off'); 
end

顺便说一下,您的代码中可能存在错误。 成功调用plot的结果是绘制到的轴手柄。 也就是说,handles.plotCDS1handles.axis1值相同。 需要进一步研究的是你得到了奇怪的行为。

最新更新