You are on page 1of 10

GUI 1

function [] = GUI_1()
% Demonstrate how to delete an entry from a uicontrol string.
% Creates a listbox with some strings in it and a pushbutton. When
user
% pushes the pushbutton, the selected entry in the listbox will be
deleted.
%
% Suggested exercise: Modify the GUI so when the user deletes a
certain
% string, the 'value' property is set to the previous string instead
of to
% the first string.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[500 500 200 260],...
'menubar','none',...
'name','GUI_1',...
'numbertitle','off',...
'resize','off');
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[10 60 180 180],...
'min',0,'max',2,...
'fontsize',14,...
'string',{'one';'two';'three';'four'});
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Delete String',...
'callback',{@pb_call,S});

function [] = pb_call(varargin)
% Callback for pushbutton, deletes one line from listbox.
S = varargin{3}; % Get the structure.
L = get(S.ls,{'string','value'}); % Get the users choice.
% We need to make sure we don't try to assign an empty string.
if ~isempty(L{1})
L{1}(L{2}(:)) = []; % Delete the selected strings.
set(S.ls,'string',L{1},'val',1) % Set the new string.
End

GUI 2

function [] =
GUI_2()
% Demonstrate
how to add a new entry to a uicontrol
string.
% Creates a listbox with some strings in it, an editbox and a
pushbutton.
% User types some text into the editbox, then pushes the pushbutton.
The
% user's text will be added to the top of the listbox.
%
% Suggested exercise: Modify the code so that hitting return after a
% string is typed performs the same task as pushing the pushbutton.
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[500 500 200 300],...
'menubar','none',...
'name','GUI_2',...
'numbertitle','off',...
'resize','off');
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[10 110 180 180],...
'min',0,'max',2,...
'fontsize',14,...
'string',{'one';'two';'three';'four'});
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[10 60 180 30],...
'fontsize',14,...
'string','New String');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Add String',...
'callback',{@ed_call,S});
function [] = ed_call(varargin)
% Callback for pushbutton, adds new string from edit box.
S = varargin{3}; % Get the structure.
oldstr = get(S.ls,'string'); % The string as it is now.
addstr = {get(S.ed,'string')}; % The string to add to the stack.
% The order of the args to cat puts the new string either on top or
bottom.
set(S.ls,'str',{addstr{:},oldstr{:}}); % Put the new string on top
-OR% set(S.ls,'str',{oldstr{:},addstr{:}}); % Put the new string on
bottom.

GUI 3

function [] =
GUI_3()
% Demonstrate
how to hide a uicontrol from the user.
% Creates a textbox and a checkbox. The state of the checkbox
determines
% whether or not the textbox is visible.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[300 300 200 130],...
'menubar','none',...
'name','GUI_3',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[10 70 180 40],...
'string','Hello',...
'backgroundcolor','r',...
'fontsize',23);
S.ch = uicontrol('style','check',...
'unit','pix',...
'position',[10 20 180 35],...
'string','
Check2hide',...
'fontsize',14);
set(S.ch,'callback',{@ch_call,S}) % Set callback.
function [] = ch_call(varargin)
% Callback for pushbutton.
S = varargin{3}; % Get the structure.
switch get(S.tx,'visible')
case 'on'
% The text is visible, make it invisible.
set(S.ch,'string','
Uncheck2show');
set(S.tx,'visible','off')
case 'off'
% The text is invisible, make it visible.
set(S.ch,'string','
Check2hide');
set(S.tx,'visible','on')
otherwise % This should never happen!
disp('Matlab entered the twilight zone, aborting.')
close(S.fh)
quit
end

GUI 4

function [] = GUI_4()
% Demonstrate how to make a multiline editbox.
% Produces a GUI with an editbox on the left and a listbox on the
right.
% The user is invited to enter text into the editbox, either hitting
return
% at the end of each line or letting it wrap automatically. When the
% button is pushed, each line of text from the editbox is placed as
an
% entry into the listbox. Notice the difference between how a
wrapped line
% is treated and a returned line is treated in the lisbox.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[450 450 400 200],...
'menubar','none',...
'name','Verify Password.',...
'resize','off',...
'numbertitle','off',...
'name','GUI_4');
S.ed = uicontrol('style','edit',...
'units','pix',...
'position',[10 60 190 120],...
'min',0,'max',2,... % This is the key to multiline
edits.
'string',{'Enter text here'; 'then push the
button.'},...
'fontweight','bold',...
'horizontalalign','center',...
'fontsize',11);
S.ls = uicontrol('style','list',...
'units','pix',...
'position',[210 60 180 120],...
'backgroundcolor','w',...
'HorizontalAlign','left');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 380 40],...
'HorizontalAlign','left',...
'string','Transfer',...
'fontsize',14,'fontweight','bold',...
'callback',{@pb_call,S});
uicontrol(S.ed)
% Give the editbox control.

function [] = pb_call(varargin)
% Callback for edit.
S = varargin{3};
% Get the string from the edit box. Note that since the editbox is a
% multiline editbox (max-min>2), the string returned is a cell array.
E = get(S.ed,'string');
set(S.ls,'string',E) % Now set the listbox string to the value in E.

GUI 5

function [] = GUI_5()
% Demonstrate how to use a pushbutton to delete bits of string and
how to
% let the user know that their actions are futile. After the string
is
% deleted completely, the user is informed that there is nothing left
to
% delete if the delete button is pressed again. A color change
accompanies
% this announcement.
%
% Suggested exercise: Add a counter to S that starts incrementing
when the
% warning is given. If the user clicks again, close the GUI.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','GUI_5',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[20 10 260 30],...
'string','Deleter');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[20 50 260 30],...
'fontsize',16,...
'string','DeleteMe');
set(S.pb,'callback',{@pb_call,S}) % Set the callback for pushbutton.

function [] = pb_call(varargin)
% Callback for the pushbutton.
S = varargin{3}; % Get the structure.
T = get(S.tx,'string'); % Get the current string.
if isempty(T)
set(S.pb,'backgroundcolor',[1 .5 .5],'string','Nothing to
Delete!')
else
set(S.tx,'str',T(1:end-1)); % Delete the last character in
string.
end

GUI 6
function [] =
% Demonstrate
% Creates two
clicked
% shows which

GUI_6()
how to update one uicontrol with data from others.
radiobuttons and a pushbutton. The pushbutton, when
radio button (or both or none) is currently selected.

See
% GUI_8 for similar radiobuttongroup GUI.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[400 400 120 100],...
'menubar','none',...
'name','GUI_6',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[10 10 100 20],...
'string','None Selected',...
'tooltip','Push to find out which radio button is
selected');
S.rd(1) = uicontrol('style','rad',...
'unit','pix',...
'position',[10 40 100 20],...
'string',' Button A');
S.rd(2) = uicontrol('style','rad',...
'unit','pix',...
'position',[10 70 100 20],...
'string',' Button B');
set(S.pb,'callback',{@pb_call,S}); % Set the callback, pass hands.
function [] = pb_call(varargin)
% Callback for pushbutton.
S = varargin{3}; % Get structure.
R = [get(S.rd(1),'val'), get(S.rd(2),'val')];
str = 'Both selected'; % Default string.

% Get state of radios.

if R(1)==1 && R(2)==0


str = 'A selected';
elseif R(1)==0 && R(2)==1
str = 'B selected';
elseif ~any(R)
str = 'None selected';
end
set(S.pb,'string',str)

GUI 7
function [] = GUI_7()
% Demonstrate how to store choice counters for multiple user choices.
% Creates a popup with two choices and a textbox to display the
number of
% times each choice has been made.
%

%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','GUI_7',...
'numbertitle','off',...
'resize','off');
S.tx = uicontrol('style','tex',...
'unit','pix',...
'position',[10 15 280 20],...
'backgroundcolor',get(S.fh,'color'),...
'fontsize',12,'fontweight','bold',...
'string','OPTION 1: 0
OPTION 2: 0');
S.pp = uicontrol('style','pop',...
'unit','pix',...
'position',[10 60 280 20],...
'backgroundc',get(S.fh,'color'),...
'fontsize',12,'fontweight','bold',...
'string',{'option 1';'option 2'},'value',1);
S.CNT = [0 0]; % Holds the number of times each option has been
called.
set(S.pp,'callback',{@pp_call,S}); % Set the callback.

function [] = pp_call(varargin)
% Callback for popupmenu.
S = varargin{3}; % Get the structure.
P = get(S.pp,'val'); % Get the users choice from the popup.
S.CNT(P) = S.CNT(P) + 1; % Increment the counter.
set(S.tx, 'string', sprintf('OPTION 1: %i
OPTION 2: %i',
S.CNT));
set(S.pp,'callback',{@pp_call,S}); % Save the new count.

GUI 8
function [] = GUI_8()
% Demonstrate how to tell which button in a uibuttongroup is
selected.
% Similar to GUI_6 except that a uibuttongroup which enforces
exclusivity
% is used.
%
% Suggested exercise: Make the editbox change the selected
radiobutton.
% Be sure to check that user input is valid.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...

'position',[300 300 250 200],...


'menubar','none',...
'name','GUI_8',...
'numbertitle','off',...
'resize','off');
S.bg = uibuttongroup('units','pix',...
'pos',[20 100 210 90]);
S.rd(1) = uicontrol(S.bg,...
'style','rad',...
'unit','pix',...
'position',[20 50 70 30],...
'string','Radio 1');
S.rd(2) = uicontrol(S.bg,...
'style','rad',...
'unit','pix',...
'position',[20 10 70 30],...
'string','Radio 2');
S.rd(3) = uicontrol(S.bg,...
'style','rad',...
'unit','pix',...
'position',[120 50 70 30],...
'string','Radio 3');
S.rd(4) = uicontrol(S.bg,...
'style','rad',...
'unit','pix',...
'position',[120 10 70 30],...
'string','Radio 4');
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[100 60 50 30],...
'string','1');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[75 20 100 30],...
'string','Get Current Radio',...
'callback',{@pb_call,S});

function [] = pb_call(varargin)
% Callback for pushbutton.
S = varargin{3}; % Get the structure.
% Instead of switch, we could use num2str on:
% find(get(S.bg,'selectedobject')==S.rd)
(or similar)
% Note the use of findobj. This is because of a BUG in MATLAB,
whereby if
% the user selects the same button twice, the selectedobject property
will
% not work correctly.
switch findobj(get(S.bg,'selectedobject'))
case S.rd(1)
set(S.ed,'string','1') % Set the editbox string.
case S.rd(2)
set(S.ed,'string','2')
case S.rd(3)
set(S.ed,'string','3')
case S.rd(4)
set(S.ed,'string','4')
otherwise
set(S.ed,'string','None!') % Very unlikely I think.

end

GUI 9
function [] = GUI_9()
% Demonstrate one way to let the user know a process is running.
% Creates a pushbutton which, when pushed, simulates some process
running
% in the background and lets the user know this is happening by a
text and
% color change. When the process is finished, the button returns to
% normal. CAREFULLY READ THE COMMENTS BELOW IF YOU PLAN ON USING
THIS
% METHOD IN ONE OF YOUR GUIs.
%
%
% Author: Matt Fig
% Date: 7/15/2009
S.fh = figure('units','pixels',...
'position',[300 300 200 100],...
'menubar','none',...
'name','GUI_9',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[20 20 160 60],...
'string','Push Me',...
'callback',{@pb_call},...
'backgroundc',[0.94 .94 .94],...
'busyaction','cancel',...% So multiple pushes don't
stack.
'interrupt','off');

function [] = pb_call(varargin)
% Callback for pushbutton.
h = varargin{1}; % Get the caller's handle.
col = get(h,'backg'); % Get the background color of the figure.
set(h,'str','RUNNING...','backg',[1 .6 .6]) % Change color of button.
% The pause (or drawnow) is necessary to make button changes appear.
% To see what this means, try doing this with the pause commented
out.
pause(.01) % FLUSH the event queue, drawnow would work too.
% Here is where you put whatever function calls or processes that the
% pushbutton is supposed to activate.
% Next we simulate some running process. Here just sort a vector.
A = rand(3000000,1);
A = sort(A); %#ok
set(h,'str','Push Me','backg',col) % Now reset the button features.

GUI 10

function [] =
GUI_10()
% Demonstrate how to make an image visible or invisible by
pushbutton.
% Pushing the pushbutton makes the image appear and disappear to the
user.
% Many people have trouble with this because just setting the axes
property
% does not do the job.
%
%
% Author: Matt Fig
% Date: 1/15/2010
S.fh = figure('units','pixels',...
'position',[200 200 200 200],...
'menubar','none',...
'numbertitle','off',...
'name','GUI_10',...
'resize','off');
S.ax = axes('units','pixels',...
'position',[30 50 160 140]);
S.im = load('clown'); % This is a built-in ML example.
S.R = image(S.im.X); % Display the image on S.ax.
colormap(S.im.map); % Set the figure's colormap.
set(S.ax,'xtick',[],'ytick',[]) % Get rid of ticks.
S.pb = uicontrol('style','push',...
'units','pixels',...
'position',[10 10 180 30],...
'fontsize',14,...
'string','INVISIBLE/VISIBLE',...
'callback',{@pb_call,S});
function [] = pb_call(varargin)
% Callback for the pushbutton.
S = varargin{3}; % Get the structure.
switch get(S.R,'visible')
case 'on'
st = 'off';
case 'off'
st = 'on';
otherwise
close(S.fh) % It would be very strange to end up here.
error('An unknown error occured in the callback')
end
set([S.R,S.ax],'visible',st) % Set BOTH the image and axis
visibility.

You might also like