Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我在两个不同的文件中有两个无花果。通过单击第一个无花果上的按钮,我想显示第二个无花果...该怎么做?可能吗?
如果是,那么如何在两个数字之间交换数据? 回答: 有多种在GUI之间共享数据的方法。通常,您需要以某种方式使一个GUI的图形句柄可用于另一GUI,以便它可以获取/设置某些对象属性。这是一个非常简单的示例,涉及一个GUI创建另一个GUI并向其传递对象句柄: function gui_one hFigure = figure('Pos',[200 200 120 70],... %# Make a new figure 'MenuBar','none'); hEdit = uicontrol('Style','edit',... %# Make an editable text box 'Parent',hFigure,... 'Pos',[10 45 100 15]); hButton = uicontrol('Style','push',... %# Make a push button 'Parent',hFigure,... 'Pos',[10 10 100 25],... 'String','Open new figure',... 'Callback',@open_gui_two); %#---Nested functions below--- function open_gui_two(hObject,eventData) gui_two(hEdit); %# Pass handle of editable text box to gui_two end end %#---Subfunctions below--- function gui_two(hEdit) displayStr = get(hEdit,'String'); %# Get the editable text from gui_one set(hEdit,'String',''); %# Clear the editable text from gui_one hFigure = figure('Pos',[400 200 120 70],... %# Make a new figure 'MenuBar','none'); hText = uicontrol('Style','text',... %# Make a static text box 'Parent',hFigure,... 'Pos',[10 27 100 15],... 'String',displayStr); end 将上面的代码保存到m文件后,您可以通过键入gui_one创建第一个GUI。您将看到一个小的图形窗口,其中包含可编辑的文本框和一个按钮。如果在文本框中键入内容,然后单击按钮,第二个GUI将出现在其旁边。第二个GUI使用从第一个GUI传递给它的可编辑文本框的句柄来获取文本字符串,显示它,并从第一个GUI清除该字符串。 这只是一个简单的例子。有关在MATLAB中对GUI进行编程的更多信息,请查看MathWorks在线文档以及该SO问题的答案中的链接。 更多&回答... |
![]() |
![]() |