Thursday, March 1, 2012

GUI in MATLAB 6

We tasted what uicontrol is like last time.
Let's see what else is available and what more we can do with it.

There are 10 different types of uicontrol - pushbutton, togglebutton, ...
I'll show you 6 types of uicontrol that I use most.

Let's start with the usual - making windows without a menubar or a toolbar, and 6 uicontrols with different styles.

Then, change the displayed strings using set(handle, 'string',...).
Also, add a local functions that will do something when the uicontrol is changed in some way

These two are the local functions for the uicontrols.

Run this, and this figure with things appear.

Now when you click the uicontrol or make changes on the uicontrol, a message will appear on the command window.
These are how to use them.

Pushbutton: click it
Checkbox: check or uncheck the box
Edit box: type something in and press enter
Text box: sorry. it's for display only. So, even though we have callback feature set up for it, it won't do anything.
Listbox: click different items
Popupmenu: use the drop down triangle on the right and click different entries.

There are a few things to note. The listbox and popupmenu have different local function set up than the rest of uicontrols. Do you wonder why? Change the callback to user 1st local function and see what happens when you play with listbox or popupmenu.


Next time I'll show you how to save information on the hidden storage of the figure.



This is the source code for this exercise.

function GUI2
% Start with the usual.
% Open a window, get rid of the menubar and toolbar.
F1 = figure('Position',[100 300 700 250],'menubar','none','toolbar','none');

% Add differnt kinds of uicontols. Handles are S#. S for style
S1 = uicontrol('unit','pixel','Position',[10 210 680 30],...
    'style','pushbutton');
S2 = uicontrol('unit','pixel','Position',[10 170 680 30],...
    'style','checkbox');
S3 = uicontrol('unit','pixel','Position',[10 130 680 30],...
    'style','edit');
S4 = uicontrol('unit','pixel','Position',[10 90  680 30],...
    'style','text');
S5 = uicontrol('unit','pixel','Position',[10 50  680 30],...
    'style','listbox');
S6 = uicontrol('unit','pixel','Position',[10 10  680 30],...
    'style','popupmenu');

% Set strings on the uicontrol
set(S1, 'string','This is pushbutton')
set(S2, 'string','This is checkbox')
set(S3, 'string','This is edit box')
set(S4, 'string','This is text box')
set(S5, 'string','This is listbox-line1 | line2 | line3 | line4')
set(S6, 'string','This is popupmenu-line1 | line2 | line3 | line4')

% Configure what's going to happen if you click, change, or something.
set(S1, 'callback',{@LOCALReaction})
set(S2, 'callback',{@LOCALReaction})
set(S3, 'callback',{@LOCALReaction})
set(S4, 'callback',{@LOCALReaction})
set(S5, 'callback',{@LOCALReaction2})
set(S6, 'callback',{@LOCALReaction2})

function LOCALReaction(varargin)
Handle = varargin{1};
disp(['You''re playing with ',get(Handle,'Style'),', and the contents are:'])
disp(get(Handle,'string'))
disp(' ')

function LOCALReaction2(varargin)
Handle = varargin{1};
disp(['You''re playing with ',get(Handle,'Style'),', and the value is:'])
disp(get(Handle,'value'))
disp(' ')







No comments:

Post a Comment