简体   繁体   English

如何从Octave图中删除GUI元素?

[英]How to remove GUI element from Octave figure?

Relatively new to Octave, and I'm creating a straightforward figure window with a plot in an axis object and some uicontrol objects that can manipulate the plot. 相对较新的Octave,我正在创建一个简单的数字窗口,其中包含一个轴对象中的绘图和一些可以操纵绘图的uicontrol对象。 All very straightforward up to this point, I make the figure, axes, and uicontrols 到目前为止,我都非常直截了当地制作了数字,轴和uicontrols

figure(1, 'position', ...
h.ax = axes(...
h.button = uicontrol('style', 'pushbutton', 'string', 'press me', 'callback', @func)
h.label = uicontrol(...
guidata(gcf, h)

The problem comes from one of my button callbacks. 问题来自我的一个按钮回调。 When the button is pressed, not only is the plot altered, but I need to remove one of the elements from the gui, for instance a uicontrol label. 按下按钮时,不仅图形被改变,而且我需要从gui中删除其中一个元素,例如uicontrol标签。 The only method I've found for taking a gui element from a figure window is to delete the uicontrol object, so my callback looks something like 我从图形窗口中获取gui元素的唯一方法是删除uicontrol对象,所以我的回调看起来像

function func (obj)
  h = guidata(obj);

  delete(h.label);
  ...

  guidata(obj, h);
endfunction

This yields "error: guidata: H must be a valid object handle execution error in graphics callback function". 这会产生“错误:guidata:H必须是图形回调函数中的有效对象句柄执行错误”。

I suspect my mistake will be obvious to someone with a grasp of how graphics handles work in Octave / Matlab. 我怀疑我的错误对于掌握Octave / Matlab图形处理方式的人来说是显而易见的。 It'd be a huge help to understand what's going wrong. 了解出了什么问题对我们来说是一个巨大的帮助。

Instead of deleting the uicontrol object, you could just set its visible property to off . 您可以将其visible属性设置为off ,而不是删除uicontrol对象。 That way, the uicontrol still exist, you just can't see it. 那样, uicontrol仍然存在,你只是看不到它。

From https://octave.org/doc/v4.2.0/Uicontrol-Properties.html : 来自https://octave.org/doc/v4.2.0/Uicontrol-Properties.html

visible : " off " | visible :“ off ”| {" on "} {“ on ”}

If visible is " off ", the uicontrol is not rendered on screen. 如果可见“ off ”,则uicontrol不会在屏幕上呈现。

Your code isn't complete, so I cannot comment on a specific bug ... however, this code works for me (in octave): 你的代码不完整,所以我不能评论一个特定的bug ...但是,这段代码适用于我(八度):

function testo()
  figure(1, 'position', [10, 10, 400, 400]);
  h.ax = axes('position', [0,0,1,1]);
  h.button = uicontrol('style', 'pushbutton', 'string', 'press me', 'position', [10, 50, 100, 50], 'callback', @func);
  h.label1 = uicontrol('style', 'text', 'string', 'label1', 'position', [120, 50, 100, 50]);
  h.label2 = uicontrol('style', 'text', 'string', 'label2', 'position', [230, 50, 100, 50]);
  guidata(gcf, h)
endfunction

function func (obj,evnt)
  h = guidata(obj);
  delete(h.label1);
  guidata(obj, h);
endfunction

Note that if you press the button again, you get the error you see. 请注意,如果再次按下该按钮,则会出现错误。 So perhaps the problem isn't deleting the label per se, but you deleting something else that doesn't actually exist. 所以问题可能不是删除标签本身,而是删除其他实际上不存在的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM