0

因此,在完成一些基本的图像处理程序(包括反转/镜像/过滤等)之后,我想创建一个包含按钮的 GUI,这些按钮可以在将要打开的图像上执行每个这些程序。在摆弄“指南”之后,我有点迷路了。

所有这些回调/createfcn/buttondownfcn 都让我有点困惑,而且我似乎无法让我的语法正确。

我尝试了各种方法来使按钮对应于按钮的按下,包括

 function X %just adding the name of a function after the green comment lines describing the three variables

但这不会产生任何结果。我的问题是,我将如何编写一个与我之前创建的功能相对应的按钮。我只是缺少某个功能还是我不理解一些更广泛的概念?

抱歉,如果问题有点模糊,我对 GUI 的了解很少。

4

2 回答 2

1

好的,我通常不关心 GUIDE GUI,但这里有一个非常简单的例子:

function varargout = untitled(varargin)
% UNTITLED M-file for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_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 untitled

% Last Modified by GUIDE v2.5 18-Jun-2013 08:52:25

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_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
end

% --- Executes just before untitled is made visible.
function untitled_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 untitled (see VARARGIN)

% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end

% --- Outputs from this function are returned to the command line.
function varargout = untitled_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;
end

% --- 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)
  % get a handle to the text field
  textDisp = get(handles.text1,'String');
  if( strcmp(textDisp{1},'Static Text' ) )
    set(handles.text1,'String',{'Checkout my new text'});
  elseif( strcmp(textDisp{1},'Checkout my new text') )
    set(handles.text1,'String',{'Clicked it 1 time'})
  else
    dd = sscanf(textDisp{1},'Clicked it %d time');
    dd = dd + 1;
    set(handles.text1,'String',{['Clicked it ' num2str(dd) ' time']})
  end
end

它将运行一个非常简单的图形,如下所示:

简单的图形用户界面

每次按下按钮时,它都会使用引起回调的对象pushbutton1_Callback的句柄来调用. 只需在回调函数或其他任何方法中进行图像处理,它就可以让你到达你要去的地方。!hObjecteventdatahandles

于 2013-06-18T14:22:06.913 回答
1

在guide中创建uiobject时,会在对应的m文件中自动生成一个回调函数。将您的函数调用放在该回调中,它会在按下按钮时执行。

如果您有其他输入,例如文本框或选择框,您可以从回调中访问它们以读入变量输入。

例如,如果您有一个带有标签的文本框textbox1,以及一个您想使用 texbox 内容的函数myfun,请将以下内容放在按钮的回调函数下:

str = get(handles.textbox1,'String');
myfun(str);
于 2013-06-18T14:30:22.227 回答