因此,在完成一些基本的图像处理程序(包括反转/镜像/过滤等)之后,我想创建一个GUI,其中包含可以在要打开的图像上执行这些程序的按钮。在摆弄“指南”之后,我有点失落。
所有这些回调/ createfcn / buttondownfcn让我有点困惑,我似乎无法使我的语法正确。
我尝试了各种方法使按钮对应按下按钮
function X %just adding the name of a function after the green comment lines describing the three variables
但这不会产生任何结果。我的问题是,如何编程按钮以与我之前创建的功能相对应。我只是缺少某种功能,还是我不理解一些更广泛的概念?
对不起,如果问题有点模糊,我对GUI的了解相当小。
答案 0 :(得分: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
的{{1}},导致回调的对象的句柄,按钮hObject
,在这种情况下它会被调用是空的,eventdata
结构在这种情况下具有GUI中所有内容的句柄。仅仅在回调函数中进行图像处理,或者它应该允许您到达目的地。 HTH!
答案 1 :(得分:1)
在指南中创建uiobject时,将在相应的m文件中自动生成回调函数。将函数调用放在该回调中,并在按下按钮时执行。
如果您有其他输入,例如文本框或选择框,则可以从回调中访问它们以读入变量输入。
例如,如果您有一个带有标记textbox1
的文本框,以及一个要使用texbox内容的函数myfun
,请将以下内容放在按钮的回调函数下:
str = get(handles.textbox1,'String');
myfun(str);