0% found this document useful (0 votes)
83 views18 pages

10 Examples of Gui

The document contains 8 examples of MATLAB graphical user interface (GUI) code. Each example creates a GUI with various controls like pushbuttons, checkboxes, radiobuttons, lists, etc. and includes callback functions to handle user interactions with the controls.

Uploaded by

Himanshu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views18 pages

10 Examples of Gui

The document contains 8 examples of MATLAB graphical user interface (GUI) code. Each example creates a GUI with various controls like pushbuttons, checkboxes, radiobuttons, lists, etc. and includes callback functions to handle user interactions with the controls.

Uploaded by

Himanshu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Examples 1 :

function [] = GUI_1()
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
Example 2

function [] = GUI_2()
S1.fh = figure('units','pixels',...
'position',[00 00 200 300],...
'menubar','none',...
'name','GUI_2',...
'numbertitle','off',...
'resize','off');
S1.ls = uicontrol('style','list','unit','pix',...
'position',[10 110 180 180],...
'min',0,'max',2,...
'fontsize',14,...
'string',{'one';'two';'three';'four'});
S1.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[10 60 180 30],...
'fontsize',14,...
'string','New String');
S1.pb = uicontrol('style','push',...
'units','pix',...
'position',[0 10 180 40],...
'fontsize',14,...
'string','Add String',...
'callback',{@ed_call,S1});
function [] = ed_call(varargin)
% Callback for pushbutton, adds new string from edit box.
S1 = varargin{3}; % Get the structure.
oldstr = get(S1.ls,'string'); % The string as it is now.
addstr = {get(S1.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(S1.ls,'str',{addstr{:},oldstr{:}}); % Put the new string on top -OR-
% set(S.ls,'str',{oldstr{:},addstr{:}}); % Put the new string on bottom.

Example 3

function [] = GUI_3()
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

Example 4:

function [] = GUI_4()
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.

Example 5

function [] = GUI_5()
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

Example 6

Selection will display on button


function [] = GUI_6()

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')]; % Get state of radios.
str = 'Both selected'; % Default string.
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)

Example 7:

Option counter will increase on each selection


function [] = GUI_7()
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.

Example 8

function [] = GUI_8()
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.

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

Example 9

function [] = GUI_9()
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(.9) % 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.

Example 10

function [] = GUI_10()

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.

Example 11

function [] = GUI_11()

S.fh = figure('units','pix',...
'pos',[400 400 120 50],...
'menubar','none',...
'name','GUI_11',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('string','Stop Loop!',...
'callback',{@pb_call},...
'units','pixels',...
'fontsize',11,...
'fontweight','bold',...
'position',[10 10 100 30]);

n = 1;
drawnow; % Draw the GUI before we enter the loop!
for ii = 1:inf % First loop, run EITHER this loop OR the next one.
if ~ishandle(S.fh) % Check if the figure exists.
break;
end
drawnow; % Try it without this line to see what happens (Ctrl+R)!
n = n + 1; % Here is where all of the loop commands would go.
end

%
fprintf('\n\t%s%i\n\n','The number of iterations completed is: ',n )

function [] = pb_call(varargin)
% Callback for pushbutton
delete(S.fh) % Delete the figure.
end
end

Example 12

function [] = GUI_12()

S.fh = figure('units','pixels',...
'position',[300 300 300 160],...
'menubar','none',...
'name','GUI_12',...
'numbertitle','off',...
'resize','off');
S.UN = get(0,'units'); % We need to temporarily change this.
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[20 30 260 100],...
'string', 'Push Me To Close Figure',...
'fontsize',12,'fontweight','bold',...
'callback',{@pb_call,S});
set(S.fh,'WindowButtonMotionFcn',{@fh_wbmf,S})
function [] = fh_wbmf(varargin)
S = varargin{3}; % Get the structure.
set(0,'units','normalized') % Temporary, see below.
% Move the mouse pointer to a random position.
set(0, 'PointerLocation', [rand rand])
set(0,'units',S.UN) % Undo change to user's system. Good courtesy.

Example 13

function [] = GUI_13()

S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','GUI_13',...
'numbertitle','off',...
'resize','off');
S.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[20 10 260 30],...
'min',1,'max',100,'val',50);
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[20 50 260 30],...
'fontsize',16,...
'string','50');
set([S.ed,S.sl],'call',{@ed_call,S}); % Shared Callback.

function [] = ed_call(varargin)
% Callback for the edit box and slider.
[h,S] = varargin{[1,3]}; % Get calling handle and structure.

switch h % Who called?


case S.ed
L = get(S.sl,{'min','max','value'}); % Get the slider's info.
E = str2double(get(h,'string')); % Numerical edit string.
if E >= L{1} && E <= L{2}
set(S.sl,'value',E) % E falls within range of slider.
else
set(h,'string',L{3}) % User tried to set slider out of range.
end
case S.sl
set(S.ed,'string',get(h,'value')) % Set edit to current slider.
otherwise
% Do nothing, or whatever.
end

Example 14

function [] = GUI_14()

S.fh = figure('units','pixels',...
'position',[300 300 230 40],...
'menubar','none',...
'name','GUI_14',...
'numbertitle','off',...
'resize','off');
% We will get different colors by using HTML.
STR = {'<HTML><FONT COLOR="#66FF66">Green</FONT></HTML>',...
'<HTML><FONT COLOR="red">Red</FONT></HTML>',...
'<HTML><FONT COLOR="blue">Blue</FONT></HTML>',...
'<HTML><FONT COLOR="#FF00FF">Violet</FONT></HTML>',...
'<HTML><FONT COLOR="black">Black</FONT></HTML>',...
'<HTML><FONT COLOR="yellow">Yellow</FONT></HTML>'};
S.COL = {'Green','Red','Blue','Violet','Black','Yellow'}; % Lookup table.
S.ls = uicontrol('style','list',...
'units','pix',...
'position',[10 10 90 25],...
'string', STR,...
'fontsize',14);
S.pb = uicontrol('style','push',...
'units','pix',...
'posit',[120 10 100 25],...
'string', 'Print Choice',...
'fontsize',10,...
'callback',{@pb_call,S});

function [] = pb_call(varargin)
% Callback for pushbutton. Displays user's choice at command line.
S = varargin{3}; % Get structure.
% If the 'value' property was used below, the user's choice might not show
% correctly if the selection was made with the arrows on the listbox.
L = get(S.ls,'listboxtop'); % Get the user's choice.
disp(['Your color choice is: ' S.COL{L}]) % Print to command line.
Example 15

function [] = GUI_15()

S.fh = figure('units','pixels',...
'position',[300 300 400 120],...
'menubar','none',...
'name','GUI_15',...
'numbertitle','off',...
'resize','off');
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[30 70 340 30],...
'string','This text can be copied but not changed');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[30 30 340 30],...
'string','Print to screen');
set([S.ed,S.pb],{'callback'},{{@ed_call,S};{@pb_call,S}}) % Set callbacks.
set(S.ed,'keypressfcn',{@ed_kpfcn,S}) % set keypressfcn.

function [] = pb_call(varargin)
% callback for pushbutton
S = varargin{3}; % Get the structure.
disp(get(S.ed,'string')) % Print to the command line.

function [] = ed_call(varargin)
% Callback for edit
S = varargin{3}; % Get the structure.
set (S.ed,'string','This text can be copied but not changed');

function [] = ed_kpfcn(varargin)
% Keypressfcn for edit
[K,S] = varargin{[2 3]};
if isempty(K.Modifier)
uicontrol(S.pb)
set (S.ed,'string','This text can be copied but not changed');
elseif ~strcmp(K.Key,'c') && ~strcmp(K.Modifier{1},'control')
uicontrol(S.pb)
set (S.ed,'string','This text can be copied but not changed');
end

Example 16

function [] = GUI_16()

S.fh = figure('units','pixels',...
'position',[300 300 390 100],...
'menubar','none',...
'name','GUI_16',...
'numbertitle','off',...
'resize','off');
S.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[60 10 270 30],...
'min',1,'max',100,'val',50);
S.ed(1) = uicontrol('style','edit',...
'unit','pix',...
'position',[10 10 40 30],...
'fontsize',12,...
'string','1'); % Displays the min.
S.ed(2) = uicontrol('style','edit',...
'unit','pix',...
'position',[60 50 270 30],...
'fontsize',16,...
'string','50'); % Displays the value.
S.ed(3) = uicontrol('style','edit',...
'unit','pix',...
'position',[340 10 40 30],...
'fontsize',12,...
'string','100'); % Displays the max.
set([S.ed(:);S.sl],'call',{@sl_call,S}); % Shared Callback.
function [] = sl_call(varargin)
% Callback for the edit box and slider.
[h,S] = varargin{[1,3]}; % Get calling handle and structure.
SL = get(S.sl,{'min','value','max'}); % Get the slider's info.
E = str2double(get(h,'string')); % Numerical edit string.

switch h % Who called?


case S.ed(1)
if E <= SL{2}
set(S.sl,'min',E) % E is less than current value.
elseif E < SL{3}
set(S.sl,'val',E,'min',E) % E is less than max value.
set(S.ed(2),'string',E) % Set the current display.
else
set(h,'string',SL{1}) % Reset the value.
end
case S.ed(2)
if E >= SL{1} && E <= SL{3}
set(S.sl,'value',E) % E falls within range of slider.
else
set(h,'string',SL{2}) % User tried to set slider out of range.
end
case S.ed(3)
if E >= SL{2}
set(S.sl,'max',E) % E is less than current value.
elseif E > SL{1}
set(S.sl,'val',E,'max',E) % E is less than max value.
set(S.ed(2),'string',E) % Set the current display.
else
set(h,'string',SL{3}) % Reset the value.
end
case S.sl
set(S.ed(2),'string',SL{2}) % Set edit to current slider.
otherwise
% Do nothing
end

You might also like