GUI之间的信息传递
1.问题描述
下面是查找过的关键词,中英文具备,但是很少能够查到答案!不过在此还是要总结一下,如果有幸能帮到别人,倒也不错!!!
1.1 搜索使用过的关键词
- matlab 使用一个GUI打开另一个GUI?
- matlab findobj为空,但是有标签?
- findobj报空 多个GUI窗口之间的控件调用
- 多个GUI窗口之间的控件调用?
- 此类型的变量不支持使用点进行索引?
- matlab 子GUI中 此类型的变量不支持使用点进行索引?
- dot indexing not supported for this type of variable?
- How to pass data from one GUI to another?
- How to call another GUI and let the rest code in GUI1 wait until GUI2 finishes executing?
- How to: Close GUI 1, transfer data to GUI 2, open GUI 2 (and back again)?
- matlab use GUI1 open GUI2?
1.2 具体描述
我想先打开一个GUI1,然后在这个GUI1中通过,点击按钮来打开GUI2,在GUI2中会展示GUI1中出现的一张图片,然后在GUI2中有一个按钮可以把这个图片下载下来?
但是在实现过程中,往往是可以在GUI1中,直接绘制GUI2中axes中的图像,但是打开GUI2之后,却不能获取到GUI2中的图像?
跳转2.3的实现。
1.3 实例展示 & 复现BUG
备注:在实例中添加了一个小小的流程,GUI1打开GUI2之后会直接在GUI2的axes1上显示GUI里面出现的图,然后GUI2中有一个显示axes2的按钮,会将axes1中的图片,显示到axes2中。
备注:右侧有一个框,录制完毕之后没有不知道应该怎么去掉,感觉好难受 。
实现过程中可能会写出来的源码:
1.3.1 案例1
GUI_1 button:【显示图片】
备注:点击按钮读取一张图片,然后在axes1中绘制出来。
% #--- 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)
global a_pic;
a_pic=imread('D:\MATLAB\DEMOCODE\Export_test\input\doll.png');
axes(handles.axes1);
imshow(a_pic);
% #备注:在matlab中注释需要使用%,但显示并非特别好看,所以都加了#注释。
GUI_1 button:【打开GUI_2】
% #--- 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)
global a_pic; %#加载的图片
open('GUI_2.fig'); %#打开GUI_2
mr_handles=guihandles; %#句柄,返回的是GUI_2的句柄
axes(mr_handles.axes1); %#定位到GUI_2的axes1
imshow(a_pic); %#在定位好的地方绘制图片
备注:上面代码仅仅是返回了GUI_2的句柄,这样将会导致一些列的问题。
GUI_2 button:【显示axes2】
%# --- 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)
im=getimage(handles.axes1);
axes(handles.axes2);
imshow(im);
会有如下报错:不支持使用点进行索引。
1.3.2 案例2
由于开始写GUI的时候就遇到过类似问题,直接打开.fig文件会有这样的问题,所以能不能打开.m的文件,于是:
open('GUI_2.m')
很遗憾的,这样是不可行的,会得到一个不太正确的句柄,实际上得到的是GUI_1的,但是只得到了figure和两个button的,没有axes的!!
然后,便开始另谋他术,使用findobj函数,在GUI中直接进行查找对应句柄,然后使用改句柄修改欲意修改的值,如下:
% #--- 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)
axes1=findobj(0,'Tag','axes1'); %#获取axes1的句柄
im=getimage(axes1);
% #im=getimage(handles.axes1);
axes(handles.axes2);
imshow(im);
但是上述的获取方法,在通过GUI_1打开GUI_2的情况下,是获取不到的!!!
但是直接打开‘GUI_2.m’文件,是可以获取到的!!!!!
】
上述代码代码带来的直接后果如下:
2.GUI之间的信息传递
2.1 通过global
- 通过global可以在同一个GUI下,不同函数之间传递信息
- 通过global可以在GUI之间,传递消息
- 备注:使用的时候,都需要使用global关键字,定义相同变量名
%#在不通过函数之间传递值
%#GUI_1 b1_Callback():GUI_1的按钮1的Callback函数
global pic;%#定义pic变量
pic=imread('one_picture.png');%#存储一张图片
%#GUI_1 b1_Callback():GUI_1的按钮2的Callback函数
global pic;%#使用全局变量pic
imshow(pic);%#展示图片'one_picture.png'
%----------------------------------------------------%
%----------------------------------------------------%
%#在GUI之间传递值
%#GUI_1 b1_Callback:GUI_1的按钮1的Callback函数
global pic;%#定义pic变量
pic=imread('one_picture.png');%#存储一张图片
%#GUI_2 b1_Callback:GUI_2的按钮1的Callback函数
global pic;%#使用全局变量pic
imshow(pic);%#展示图片'one_picture.png'
%#
备注:通过global传递方式可能不太好,也有其他传递的方式,可以参考下面的链接。
链接:Matlab GUI,全局变量,单个窗体内各个函数之间传递数据
链接:Matlab中guidata()的用法以及如何帮助参数在GUI间传递
里面提到了使用guidata进行数据存取与更新的操作,感觉可能会有用,但是还没有仔细研究。
2.2 通过setappdata & getappdata
alpha=imread('alpha.png');
%#在根路径下声明一个tag为'alpha_data',然后将alpha存储在其中
%#可以写在任意GUI的任意函数体中
setappdata(0,'alpha_data',alpha);
%#在其他的GUI中,取出tag为'alpha_data'的变量
%#同样可以写在任意位置,但最好能保证逻辑上的先set后get
alpha=getappdata(0,'alpha_data');
2.3 通过handles
继续实现1.2中的描述的功能,通过handles的相关实现如下:
GUI_1 的【打开GUI_2】按钮:
%# --- 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)
global a_pic;
handles.GUI_2_handle=[];%#空的变量,用来存储GUI_2的handle
if isempty(handles.GUI_2_handle)
handles.GUI_2_handle=GUI_2;%#打开GUI_2同时存储GUI_2的handle
end
GUI_2_data=guidata(handles.GUI_2_handle);%#获取GUI_2 handle的结构体数据
axes(GUI_2_data.axes1);%#定位到GUI_2的axes1
imshow(a_pic);%#在对应位置绘制图片
备注:虽然不明白具体的数据流动方式,但是存储了GUI_2的handle之后,在GUI_2被打开之后,不会出现1.3中的相关bug,可以直接使用dot方式获取值。
So?这是为什么呢????
在GUI_2中【显示axes2】按钮中的代码如下:
%# --- 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)
im=getimage(handles.axes1);
axes(handles.axes2);%#可以直接使用dian索引方式
imshow(im);
3.相关链接
问题性:
- How to call another GUI and let the rest code in GUI1 wait until GUI2 finishes executing?
- How to: Close GUI 1, transfer data to GUI 2, open GUI 2 (and back again)?
- how to pass data to sub-gui using guidata?
- findobj报空 多个GUI窗口之间的控件调用
解答性:
- matlab and stuff: A simple example of multiple GUIs working together[需要VPN]
- Matlab GUI界面
- MATLAB 中gcf、gca 以及gco 的区别
- Matlab-setappdata;getappdata
资料性:
4.源码
在博客中的代码是比较完整的,其实可以不用下载实例,毕竟是消耗积分的。