实现功能为:两个窗口,一个主窗口一个子窗口。主窗口中有文本框和按钮,文本框用来显示子窗口中按钮的点击次数,按钮用来打开子窗口。 子窗口中包含文本框和两个按钮,文本框显示按钮点击次数,第一个按钮用来增加点击次数,第二个按钮用来返回主窗口。
实现步骤:
(1)创建两个GUI界面,分别为主窗口和子窗口,设置好相关属性和Tag等信息:
(2)编写主程序的回调函数:(主要是添加了按钮的响应函数)
function varargout = main(varargin) % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @main_OpeningFcn, ... 'gui_OutputFcn', @main_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before main is made visible. function main_OpeningFcn(hObject, eventdata, handles, varargin) % Choose default command line output for main handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes main wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = main_OutputFcn(hObject, eventdata, handles) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) set(handles.figure1,'Visible','off');%将主窗口隐藏 times=child(handles.figure1);%调用显示子窗口并接收子窗口的返回参数 set(handles.text1,'String',num2str(times));%将文本设置为从子窗口接收过来的按钮点击次数
function varargout = child(varargin) % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @child_OpeningFcn, ... 'gui_OutputFcn', @child_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before child is made visible. function child_OpeningFcn(hObject, eventdata, handles, varargin) % Choose default command line output for child handles.output = hObject; global count;%定义全局变量用来统计按钮点击次数 count=0;%初始化 main=varargin{1};%获取主窗口传递过来的参数 handles.mainhandles=guihandles(main);%将主窗口传递过来的参数添加到handles结构体中 % Update handles structure guidata(hObject, handles);%更新handles结构体 % UIWAIT makes child wait for user response (see UIRESUME) uiwait(handles.figure1);%设置等待,等待用户进行操作 % --- Outputs from this function are returned to the command line. function varargout = child_OutputFcn(hObject, eventdata, handles) % Get default command line output from handles structure global count; varargout{1} = count;%输出返回参数,返回按钮点击次数 % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) global count; count=count+1;%按钮点击次数增加 set(handles.text1,'String',num2str(count));%设置文本为点击次数 % guidata(hObject, handles);%更新 % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) set(handles.mainhandles.figure1,'Visible','on');%将主窗口显示出来 uiresume(handles.figure1);%用户操作完成 closereq%关闭子窗口
Matlab实例学习--------不同界面的数据传递(多窗口编程)(实例)
原文地址:http://blog.csdn.net/z1137730824/article/details/38921269