由Matlabs指南构建的GUI的回调共享变量

时间:2011-04-16 00:29:49

标签: user-interface matlab callback global-variables matlab-guide

我有点发现Matlab中的GUI开发,我正在尝试一些基本概念的困难。如果有人能帮助我,我会非常感激。

我正在尝试使用matlab'guide'构建一个GUI,而我正在将图像加载到一个轴中,我想将它保存到一些全局变量中,该变量将由我GUI中的所有回调共享,我可以在其他事件处理程序上处理此图像。

我无法找到一种方法,我试图将一些变量声明为“全局”,但它没有奏效。你能解释一下它是如何工作的,或者举一个简短的例子。感谢

1 个答案:

答案 0 :(得分:3)

这是一个工作示例(使用GUIDE),它以两种不同的方式执行您正在寻找的内容。总而言之,我更喜欢在90%的GUI中使用'handle'方法。我唯一需要使用全局的是我需要访问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, eventdata, handles) 
    varargout{1} = handles.output;


% --- Executes on button press in Browse.
function Browse_Callback(hObject, eventdata, 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, eventdata, 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, eventdata, 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, eventdata, handles)
    cla(handles.axes1)