Reputation: 133
So after completing some basic image processing programs, including inversion/mirroring/filtering etc, I want to create a GUI that includes buttons that could carry out each of these programs on an image that would be opened. After fiddling around with 'guide' I have gotten a bit lost.
All of these callbacks/createfcn/buttondownfcn have me a little bit confused, and I cant seem to get my syntax correct.
Ive tried various methods to make a button correspond to the push of a button including
function X %just adding the name of a function after the green comment lines describing the three variables
But that does not yield any results. My question is, how would I program a button to correspond with a function that I have previously created. Am I simply missing a certain function or am I not understanding some broader concept?
Sorry if the question is a bit vague, my knowledge on GUI's is rather small.
Upvotes: 0
Views: 256
Reputation: 7448
When you create a uiobject in guide, a callback function will be automatically generated in the corresponding m-file. Place your function call within that callback, and it will be executed when the button is pressed.
If you have other inputs, such as a text box or a select box, you can access them from the callback to read in the variable input.
For example, if you've got a textbox with the tag textbox1
, and a function that you want to use the texbox content for, myfun
, place the following under your button's callback function:
str = get(handles.textbox1,'String');
myfun(str);
Upvotes: 1
Reputation: 4685
OK, I generally don't care for GUIDE GUI's, but here's a really simple example:
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
It's going to run off a really simple figure that looks like this:
Every time the button is pushed, it calls the pushbutton1_Callback
with the hObject
, handle of the object that caused the callback, the button, eventdata
, in this case it'll be empty, and the handles
structure which has a handle to everything in the GUI in this case. Merely do your image processing in the callback function or whatever and it should allow you to get where you're going. HTH!
Upvotes: 1