谁能告诉我如何让我的主应用程序打开一个辅助应用程序,它将捕获一些值然后将它们发送回我的主应用程序?
我知道此问题已在应用程序设计器文档中得到解决,但我无法成功实施这些步骤。另外,我尝试运行该示例,但 Matlab 说该文件不存在。如果有人可以请分享该示例,那也将非常有帮助。
谁能告诉我如何让我的主应用程序打开一个辅助应用程序,它将捕获一些值然后将它们发送回我的主应用程序?
我知道此问题已在应用程序设计器文档中得到解决,但我无法成功实施这些步骤。另外,我尝试运行该示例,但 Matlab 说该文件不存在。如果有人可以请分享该示例,那也将非常有帮助。
I have never tried to implement this on my own, but I often wandered myself how I could accomplish this if facing a complex apps architecture.
Actually, if you instantiate two GUIs in the same script/function, or if you have one GUI creating another GUI inside one of its functions, the simplest way would be to play with function handles
. For example, the first GUI can pass a function handle defined among its functions to the target GUI's constructor and, this way, the target GUI can invoke it in order to modify the first GUI's data and/or properties when necessary.
The standard approach, anyway, which is considered as a best practice, works as follows. Let's assume that you have two GUIs named G1
and G2
and that they are distinct (you are not running two instances of the same GUI). If they are both visible (HandleVisibility
set to on
) and they both have a Tag
identifier defined (G1
and G2
in our example), you can search for them within the Matlab "workspace". Hence:
% This is a G2 event handler
function pushbutton1_Callback(hObject, eventdata, handles)
g1_h = findobj('Tag','G1');
if (~isempty(g1_h))
% get all data associated to G1
g1_data = guidata(g1_h);
% modify a G2 object based on a G1 object
set(handles.MyTextBox,'String',get(g1_data.MyEditBox,'String'));
end
end
MATLAB 的 App Designer 生成基于类的 GUI,而不是 GUIDE 基于函数的 GUI。这种方法的优点是我们可以将 GUI 作为对象传递,而不必通过函数返回或按标签搜索对象之类的东西来获得创造性。
这是一个简单的程序示例,说明了此概念的一种方法。主图窗窗口打开一个辅助提示窗口,该窗口提供两个输入。当提示窗口关闭时,主 GUI 将输入值打印到命令窗口并退出。
主窗口:
classdef mainwindow < handle
properties
mainfig
butt
end
methods
function [self] = mainwindow()
% Build a GUI
self.mainfig = figure('Name', 'MainWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.butt = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.1 0.1 0.8 0.8], ...
'String', 'Push Me', 'Callback', @(h,e) self.buttoncallback);
end
function buttoncallback(self)
tmpwindow = subwindow(); % Open popupwindow
uiwait(tmpwindow.mainfig); % Wait for popup window to be closed
fprintf('Parameter 1: %u\nParameter 2: %u\n', tmpwindow.parameter1, tmpwindow.parameter2);
close(self.mainfig);
end
end
end
子窗口:
classdef subwindow < handle
properties
mainfig
label1
box1
label2
box2
closebutton
parameter1
parameter2
end
methods
function [self] = subwindow()
% Build a GUI
self.mainfig = figure('Name', 'SubWindow', 'Numbertitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none');
self.label1 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.7 0.2 0.05], ...
'String', 'Parameter 1');
self.box1 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.6 0.2 0.1], ...
'String', '10');
self.label2 = uicontrol('Parent', self.mainfig, 'Style', 'text', ...
'Units', 'Normalized', 'Position', [0.4 0.4 0.2 0.05], ...
'String', 'Parameter 2');
self.box2 = uicontrol('Parent', self.mainfig, 'Style', 'edit', ...
'Units', 'Normalized', 'Position', [0.4 0.3 0.2 0.1], ...
'String', '10');
self.closebutton = uicontrol('Parent', self.mainfig, 'Style', 'Pushbutton', ...
'Units', 'Normalized', 'Position', [0.4 0.1 0.2 0.1], ...
'String', 'Close Window', 'Callback', @(h,e) self.closewindow);
end
function closewindow(self)
% Drop our input parameters into this window's properties
self.parameter1 = str2double(self.box1.String);
self.parameter2 = str2double(self.box2.String);
% Close the window
close(self.mainfig);
end
end
end