(1) 通过guihandles获得某一窗口的handles的结构体实现对其他窗口的数据访问
示例代码:主窗口的按钮点击后主窗口消失,子窗口显示,子窗口中的按钮点击后子窗口消失,主窗口显示
主窗口代码
function varargout = mainframe(varargin)
% MAINFRAME MATLAB code for mainframe.fig
% MAINFRAME, by itself, creates a new MAINFRAME or raises the existing
% singleton*.
%
% H = MAINFRAME returns the handle to a new MAINFRAME or the handle to
% the existing singleton*.
%
% MAINFRAME('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAINFRAME.M with the given input arguments.
%
% MAINFRAME('Property','Value',...) creates a new MAINFRAME or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before mainframe_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to mainframe_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help mainframe
% Last Modified by GUIDE v2.5 02-Oct-2017 15:19:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mainframe_OpeningFcn, ...
'gui_OutputFcn', @mainframe_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 mainframe is made visible.
function mainframe_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to mainframe (see VARARGIN)
% Choose default command line output for mainframe
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mainframe wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = mainframe_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%设置按钮点击之后主窗口消失
set(handles.figure1,'Visible','off');
%设置主窗口消失之后,子窗口显示,其中ch1是子窗口对应的文件名
ch1(handles.figure1);
子窗口代码
function varargout = ch1(varargin)
% CH1 MATLAB code for ch1.fig
% CH1, by itself, creates a new CH1 or raises the existing
% singleton*.
%
% H = CH1 returns the handle to a new CH1 or the handle to
% the existing singleton*.
%
% CH1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CH1.M with the given input arguments.
%
% CH1('Property','Value',...) creates a new CH1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ch1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ch1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ch1
% Last Modified by GUIDE v2.5 02-Oct-2017 15:13:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ch1_OpeningFcn, ...
'gui_OutputFcn', @ch1_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 ch1 is made visible.
function ch1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ch1 (see VARARGIN)
% Choose default command line output for ch1
handles.output = hObject;
% 获取到传递过来的参数
wind = varargin{1};
% 把传递过来的参数赋值给handles的mainhandles
handles.mainhandles = guihandles(wind);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ch1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ch1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%设置mainhandles的figure1的Visible属性为on使主窗口再次显示,因为handles的mainhandles已经被重新赋值为主窗口的值
set(handles.mainhandles.figure1,'Visible','on');
closereq;
方法小结:实现主窗口中按钮点击后,主窗口消失,子窗口显示,子窗口中按钮点击后子窗口消失,主窗口显示的方法是:在主窗口中点击按钮后设置主窗口的Visible属性为off,并调用子窗口,把主窗口的handles.figure传递给子窗口,在子窗口中,接收主窗口中传递的参数,当子窗口中点击按钮后,设置主窗口显示,关闭子窗口。
(2) 有输出参数的多窗口编程
示例代码:在主窗口中有一个静态文本框和一个按钮,点击按钮,主窗口消失,子窗口出现,在子窗口中有两个按钮一个静态文本框,点击第一个按钮,静态文本框中显示点击的次数,点击第二个按钮,子窗口消失,主窗口显示,主窗口中的静态文本框中显示子窗口中的点击次数。
主窗口代码:
function varargout =mainframe2(varargin)
% MAINFRAME2 MATLAB code for mainframe2.fig
% MAINFRAME2,by itself, creates a new MAINFRAME2 or raises the existing
% singleton*.
%
% H =MAINFRAME2 returns the handle to a new MAINFRAME2 or the handle to
% the existingsingleton*.
%
% MAINFRAME2('CALLBACK',hObject,eventData,handles,...) calls the local
% functionnamed CALLBACK in MAINFRAME2.M with the given input arguments.
%
% MAINFRAME2('Property','Value',...) creates a new MAINFRAME2 or raisesthe
% existingsingleton*. Starting from the left,property value pairs are
% applied tothe GUI before mainframe2_OpeningFcn gets called. An
% unrecognizedproperty name or invalid value makes property application
% stop. All inputs are passed tomainframe2_OpeningFcn via varargin.
%
% *See GUIOptions on GUIDE's Tools menu. Choose"GUI allows only one
% instance torun (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to helpmainframe2
% Last Modified by GUIDE v2.5 02-Oct-2017 15:23:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn',@mainframe2_OpeningFcn, ...
'gui_OutputFcn', @mainframe2_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 mainframe2 is made visible.
functionmainframe2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handleto figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin commandline arguments to mainframe2 (see VARARGIN)
% Choose default command line output for mainframe2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mainframe2 wait for user response (seeUIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to thecommand line.
function varargout =mainframe2_OutputFcn(hObject, eventdata, handles)
% varargout cellarray for returning output args (see VARARGOUT);
% hObject handleto figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
functionpushbutton1_Callback(hObject, eventdata, handles)
% hObject handleto pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%设置主窗口的Visible属性为off,使主窗口消失
set(handles.figure1,'Visible','off');
%调用子窗口,其中ch2是子窗口的文件的名字,使用times接收返回值
times = ch2(handles.figure1);
%把接收的times的值更新到静态文本框中
set(handles.text2,'String',num2str(times));
子窗口代码
function varargout = ch2(varargin)
% CH2 MATLAB code for ch2.fig
% CH2, by itself, creates a new CH2 or raises the existing
% singleton*.
%
% H = CH2 returns the handle to a new CH2 or the handle to
% the existing singleton*.
%
% CH2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CH2.M with the given input arguments.
%
% CH2('Property','Value',...) creates a new CH2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ch2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ch2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ch2
% Last Modified by GUIDE v2.5 02-Oct-2017 15:24:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ch2_OpeningFcn, ...
'gui_OutputFcn', @ch2_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 ch2 is made visible.
function ch2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ch2 (see VARARGIN)
% Choose default command line output for ch2
handles.output = hObject;
% 接收从主窗口传递的参数
wind = varargin{1};
% 把从主窗口接收到的参数赋值给handles.mainhandles
handles.mainhandles = guihandles(wind);
% 初始化handles.times为0
handles.times = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ch2 wait for user response (see UIRESUME)
%等待用户操作结束
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ch2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
%varargout{1} = handles.output;
%set output is handles.output
%设置输出参数是handles.times
varargout{1} = handles.times;
% 关闭窗口
delete(handles.figure1);%close window
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%更新times
handles.times = handles.times + 1;
% 更新handles结构体
guidata(hObject,handles);
% 把times的值显示在静态文本框中
set(handles.text2,'String',num2str(handles.times));
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%设置主窗口显示,因为子窗口接收了从主窗口中传递的值,所以这样使用handles.mainhanles.figure1设置的是主窗口
set(handles.mainhandles.figure1,'Visible','on');
% 刷新
uiresume(handles.figure1);%refresh
方法小结:实现这个任务的关键点有两个。一是主窗口消失,子窗口显示;二是子窗口消失,主窗口显示,并且在主窗口中显示子窗口的点击次数。主窗口消失,子窗口显示可以使用visible属性设置达到效果。子窗口消失,主窗口显示,并且在主窗口中显示子窗口的点击次数这个功能可以使用返回值的方式,在主窗口中接受返回值,并显示在静态文本框中。