动态添加字段到 MATLAB GUI?

发布于 2024-07-26 12:05:17 字数 77 浏览 1 评论 0原文

我正在使用 GUIDE 生成 MATLAB GUI,但我想在用户单击按钮时创建字段。 有什么方法可以在回调中动态添加新的 GUI 对象吗?

I'm generating a MATLAB GUI using GUIDE, but I want to create fields when a user clicks on a button. Is there any way to dynamically add new GUI objects in the callbacks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-08-02 12:05:18

使用 UICONTROL,您将能够添加“字段”(称为 uicontrol 或小部件)。

您将需要指定样式来获取编辑框、按钮等...

您实际上可能希望 GUIDE 中已有所有小部件,然后只需更改可见性启用< /em> 根据需要属性。

您可以在这里找到我关于在 MATLAB 中构建 GUI 的视频教程:
http://blogs.mathworks.com/videos/category/gui-or -guide/

这应该涵盖这个以及 GUI 构建中的许多相关主题。

Using UICONTROL, you will be able to add 'fields' (called uicontrols or widgets).

You will want to specify the style to get edit boxes, buttons, etc...

You may actually want to have all the widgets already there in GUIDE and then just change the visibility or enabled property as needed.

You can find my video tutorials on GUI building in MATLAB here:
http://blogs.mathworks.com/videos/category/gui-or-guide/

This should cover this and many related topics in GUI building.

前事休说 2024-08-02 12:05:17

实现此目的的一种方法是在开始时创建 GUI 对象,但将其“Visibility”属性设置为“off”。 然后,当用户单击按钮时,您将“Visibility”属性设置回“on”。 这样,您就不会在 GUI 运行时创建新的 GUI 对象,您只需更改它的哪些部分可见或不可见。

编辑:如果您在运行时之前不知道需要多少个新的 GUI 对象,则可以通过以下方式将新的 GUI 对象添加到句柄结构中(其中 hFigure 是 GUI 图形的句柄):

p = uicontrol(hFigure,'Style','pushbutton','String','test',...
              'Callback',@p_Callback);  % Including callback, if needed
handles.test = p;  % Add p to the "test" field of the handles structure
guidata(hFigure,handles);  % Add the new handles structure to the figure

当然,您必须为新的 GUI 对象编写回调函数(如果需要的话),它可能看起来像这样:

function p_Callback(hObject,eventdata)
  handles = guidata(gcbf);  % This gets the handles structure from the figure
  ...
  (make whatever computations/changes to GUI are needed)
  ...
  guidata(gcbf,handles);  % This is needed if the handles structure is modified

我在上面的代码中使用的感兴趣的函数是: GUIDATA (用于存储/检索GUI)和 GCBF (获取父图的句柄当前正在执行回调的对象)。

One way to accomplish this is to create the GUI objects at the start, but set their "Visibility" property to "off". Then, when the user clicks on a button, you set the "Visibility" property back to "on". This way, you won't be making new GUI objects while the GUI is running, you would simply be changing which parts of it are visible or not.

EDIT: If you don't know how many new GUI objects you need until run-time, this is how you would add the new GUI objects to the handles structure (where hFigure is a handle to the GUI figure):

p = uicontrol(hFigure,'Style','pushbutton','String','test',...
              'Callback',@p_Callback);  % Including callback, if needed
handles.test = p;  % Add p to the "test" field of the handles structure
guidata(hFigure,handles);  % Add the new handles structure to the figure

You would then of course have to write the callback function for the new GUI object (if it needs one), which might look something like this:

function p_Callback(hObject,eventdata)
  handles = guidata(gcbf);  % This gets the handles structure from the figure
  ...
  (make whatever computations/changes to GUI are needed)
  ...
  guidata(gcbf,handles);  % This is needed if the handles structure is modified

Functions of interest that I used in the above code are: GUIDATA (for storing/retrieving data for a GUI) and GCBF (get handle of parent figure of the object whose callback is currently executing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文