如何解决由Matlabs指南构建的GUI的回调共享的变量
|| 我有点发现Matlab中的GUI开发,并尝试了一些基本概念的困难。如果有人可以帮助我,我将非常感激。 我正在尝试使用matlab \'guide \'构建一个GUI,而我所要做的就是将图像加载到轴中,并且我想将其保存到一些全局变量中,这种方式将由GUI中的所有回调共享我可以在其他事件处理程序上处理此图像。 我在寻找解决方法时遇到麻烦,我尝试将某些变量声明为'global',但是没有成功。您能给我解释一下它是如何工作的,或举一个简单的例子。谢谢解决方法
这是一个工作示例(使用GUIDE),它以两种不同的方式完成您要查找的内容。总的来说,我更喜欢对90%的GUI使用\'handles \'方法。我唯一一次使用全局变量是,如果我需要在GUI之外访问数据。
请注意,我已经在打开函数中添加了\'handles.img = 0 \'。作为免责声明,浏览器中没有数据验证。我也仅使用.gif文件进行了测试,并且没有考虑将图像显示到轴的最佳方法。只是又快又脏:)
编辑:当您将这些数据复制并粘贴到M文件中时。确保将其命名为picture_loader.m。 Matlab用错误的文件名对我做了一些愚蠢的事情:)
希望这可以帮助。
function varargout = picture_loader(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(\'gui_Name\',mfilename,...
\'gui_Singleton\',gui_Singleton,...
\'gui_OpeningFcn\',@picture_loader_OpeningFcn,...
\'gui_OutputFcn\',@picture_loader_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 picture_loader is made visible.
function picture_loader_OpeningFcn(hObject,eventdata,handles,varargin)
handles.output = hObject;
handles.img = 0; % Add the img data to the handle
guidata(hObject,handles);
% --- Outputs from this function are returned to the command line.
function varargout = picture_loader_OutputFcn(hObject,handles)
varargout{1} = handles.output;
% --- Executes on button press in Browse.
function Browse_Callback(hObject,handles)
global img % Store the data global
img_path = uigetfile(\'*.gif\'); % browse for a file
img = importdata(img_path); % Load the image data
handles.img = img; % Store the img data in the handles struct
guidata(hObject,handles); % Save handles so all call backs have the updated data
% Plot the data in the axes1
axes(handles.axes1) % Select axes1 to write to
image(img.cdata) % Display the image
colormap(img.colormap) % Apply proper colormap
% --- Executes on button press in load_global.
function load_global_Callback(hObject,handles)
global img
if isstruct(img)
axes(handles.axes1) %Select the axes1 on the gui
image(img.cdata)
colormap(img.colormap)
end
% --- Executes on button press in load_global_handle.
function load_handle_Callback(hObject,handles)
if isstruct(handles.img)
axes(handles.axes1) %Select the axes1 on the gui
image(handles.img.cdata)
colormap(handles.img.colormap)
end
% --- Executes on button press in clear.
function clear_Callback(hObject,handles)
cla(handles.axes1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。