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

发布于 2024-11-02 04:47:46 字数 251 浏览 0 评论 0原文

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

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

我无法找到一种方法来做到这一点,我试图将一些变量声明为“全局”,但它不起作用。您能否解释一下它是如何工作的,或者举一个简短的例子。谢谢

Im kinda discovering GUI developement in Matlab and Im experimenting difficulties with some basic concepts. If anyone could help me I would be really greatful.

Im trying to build a GUI using matlab 'guide', and all I am ding is loading an image into an axes and I want to save it into some global variable that will be shared by all the callbacks in my GUI, this way i could process this image on other event handlers.

Im having trouble finding a way to do it, I tried to declare some variables as 'global' but it didn't worked. Can you please explain me how it works, or show a brief example. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2024-11-09 04:47:46

这是一个工作示例(使用 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, 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)

Here is a working example (using GUIDE) that does what you're looking for in two different ways. Over all I prefer using the 'handles' method for 90% of my GUIs. The only time I will ever use a global is if I need to access the data outside of the GUI.

Please note that I have added the 'handles.img = 0' inside the opening function. As a disclaimer, there is no data validation from the browse. I have also only tested this with a .gif file and put no thought into the best way to display the image to the axes. Just quick and dirty :)

EDIT: When you copy and paste this data into an M-File. Be sure to name it picture_loader.m. Matlab has done some stupid things to me with the wrong file names :)

Hope this helps.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文