【问题标题】:Plot legend upon checkbox activation in GUI在 GUI 中激活复选框后绘制图例
【发布时间】:2018-01-28 08:05:30
【问题描述】:

我是 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 个复选框。请帮忙...谢谢

【问题讨论】:

    标签: matlab plot matlab-guide


    【解决方案1】:

    每个轴都需要单独的图例。您可以在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 的值相同。需要进一步研究的是你的行为很奇怪。

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 2014-05-27
      • 2012-02-19
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多