GUI Examples #2: Explore simple string manipulation

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

Pushing the pushbutton adds to the string elements of a listbox.  The added string can be chosen by the user through an editbox.

GUIExample2

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.

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

Related Posts

Comments

Comments are closed.