【问题标题】:make axes invisible or delete plot completely使轴不可见或完全删除绘图
【发布时间】:2012-08-08 13:24:02
【问题描述】:

我有一个包含 4 个绘图的 matlab gui。如果在列表中选择了不同的文件,则应更新第一个图。其他 3 个应仅在请求时可见(并被计算)。

但是,在绘制一次后,我未能使图 2-4 不可见。

我试过了

set(handles.axesImage, 'Visible', 'off');

但这只会删除轴,而不是整个图。

编辑: 除了使事物不可见之外,是否也可以实际删除内容?通常我会打电话给close(hfig);,但这里我没有数字。

我试过了

handles2hide = [axisObj;cell2mat(get(axisObj,'Children'))]; 
delete(handles2hide);

但是对于未绘制的轴(启动后)失败

编辑: 我将代码更改为:

axisObj = handles.axesContour;
if ishandle(axisObj)
    handles2delete = get(axisObj,'Children');
    delete(handles2delete);
    set(axisObj,'visible','off') 
end
if (isfield(handles,'contour') && isfield(handles.contour,'hColorbar'))
    delete(handles.contour.hColorbar);
    delete(handles.contour.hColorbarLabel);
end

但是,颜色栏仍未删除,handles.contour.hColorbar 失败并显示Invalid handle object.

【问题讨论】:

  • 删除对我有用。也许您想检查您是否只删除有效句柄,即delete(handles2hide(ishandle(handles2hide)))
  • 在此之前我收到一个错误:??? Cell contents reference from a non-cell array object. in line handles2hide = [cell2mat(get(axisObj,'Children'))];。我不得不从删除列表中删除axisObj,因为我无法再绘制到已删除的轴...
  • 我删除了cell2mat,现在情节被删除了,但是颜色条和颜色标签仍然存在。即使我保存它们的句柄以删除它们,这也不起作用,因为删除失败是因为它们未被识别为句柄。

标签: matlab axes


【解决方案1】:

您不仅要隐藏坐标轴,还要隐藏它们的所有子级:

handles2hide = [handles.axesImage;cell2mat(get(handles.axesImage,'Children'))];
set(handles2hide,'visible','off')

只有在handles.axesImage中存储了多个句柄时才需要cell2mat

请注意,您需要完整的句柄列表才能使所有内容再次可见。

编辑

如果要删除图形上的所有轴(包括颜色条)及其子项,可以执行以下操作(如果必须排除某些轴,可以在句柄列表上使用setdiff):

ah = findall(yourFigureHandle,'type','axes')
if ~isempty(ah)
   delete(ah)
end

【讨论】:

    【解决方案2】:

    我用这个:

        set(allchild(handles.axes1),'visible','off'); 
        set(handles.axes1,'visible','off'); 
    

    为了隐藏我的斧头。 我在这里找到了解决方案: Visible axes off

    【讨论】:

      【解决方案3】:

      您已经使用了子图和图的句柄:

      h(1)=subplot(221);
          p(1)=plot(rand(10,1));
      h(2)=subplot(222);
          p(2)=plot(rand(10,1));
      h(3)=subplot(223);
          p(3)=plot(rand(10,1));
      h(4)=subplot(224);
          p(4)=plot(rand(10,1));
      
      
      set([h(2) p(2)],'visible','off')
      

      隐藏第二个情节。然而,@Jonas 的答案似乎更完整。这当然更容易,因为您不必像我在这里那样手动收集孩子。

      【讨论】:

      • h(2)=subplot(222);plot(rand(10)); set(h(2),'visible','off') 是问题所在,如果我正确理解 OP
      • @Jonas 是的,在扩展我的测试时也遇到了这个问题......更新^^
      • 我在代码中向我的 gui 添加了一个子图:这只会使整个 gui 混乱并使所有轴句柄无效 -> 没有进一步的工作(我不知道)无法使用。
      【解决方案4】:

      我现在解决了

      function z_removePlots(handles)
      
      if (isfield(handles,'image') && isfield(handles.image,'hplot'))
          if ishandle(handles.image.hplot)
              delete(handles.image.hplot);
              delete(findall(gcf,'tag','Colorbar'));
              handles.image.hplot = 0;
              set(handles.axesImage, 'Visible', 'off');
          end
      end
      if (isfield(handles,'contour') && isfield(handles.contour,'hplot'))
          if ishandle(handles.contour.hplot)
              delete(handles.contour.hplot);
              handles.contour.hplot = 0;
              ClearLinesFromAxes(handles.axesContour)
              set(handles.axesContour, 'Visible', 'off');
          end
      end
      guidata(handles.output,handles);
      

      function ClearLinesFromAxes(axisObj)
      if ishandle(axisObj)
          handles2delete = get(axisObj,'Children');
          delete(handles2delete);
      end
      

      【讨论】:

        猜你喜欢
        • 2022-07-19
        • 1970-01-01
        • 1970-01-01
        • 2015-02-23
        • 2010-11-12
        • 1970-01-01
        • 1970-01-01
        • 2016-05-13
        • 2014-07-20
        相关资源
        最近更新 更多