GUI Examples #6: Explore selection determination

November 8, 2009 by Matt Fig · Leave a Comment
Filed under: GUI, Tutorials 
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 10.0/10 (1 vote cast)

Pushing the pushbutton reveals which, if any, of the radiobuttons is currently selected.

GUIExample6

function [] = GUI_6()
% Demonstrate how to update one uicontrol with data from others.
% Creates two radiobuttons and a pushbutton.  The pushbutton, when clicked
% shows which 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')]% 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)

Download full list of examples

VN:F [1.8.8_1072]
Rating: 10.0/10 (1 vote cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS

GUI Examples #5: Explore simple string manipulation and user notification

November 1, 2009 by Matt Fig · Leave a Comment
Filed under: GUI, Tutorials 
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)

A pushbutton deletes the text in a textbox one character at a time. When the string is empty, the user is notified of this fact.

GUIExample5

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

Download full list of examples

VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Live
  • PDF
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Add to favorites
  • email
  • MySpace
  • RSS