Saturday, March 10, 2012

GUI in MATLAB 7

I've been super busy with things, and haven't posted anything new for some time.
I'm back now.

Today's portion is about saving data (whatever type it is) in a hidden storage of a figure.
Why do we need this? Well, let's think about a hypothetical situation where you're dealing with a data of some kind.

You make a uicontrol button that opens up and load data from an excel file. This is done by a local function.
Then, you need to send it to the main function, so the main function can play with the data some place else.
The main function can send it to another local function when you press another function, and this local function will plot it on an axes or filter it out using whatever tools it has.

Of course you can think of sending your data out and returning it back to main function from the local function. However, this can easily make your code horribly complicated (well, at least that's what I experienced).

So, what if we save all the common data in one place, and different local functions can go grab it whenever they need it? There's no need to pass data from one location to another in the function, because the "storage space" is there holding it.
Ah~.

That's what we're doing this time and next.
I tried to put it in one posting, but I figured it's better to split into two.

So, let's start.

Before using the storage, you need to know where it is.
The answer is: anything you see on the screen. This storage is hidden, so you won't see it unless you go dig it out.
Anything you see on the screen... well, almost anything.
That includes your monitor screen, the figure window you just opened, the axes you just placed on that figure, the plot you just drew. They all have their own storage spaces.

Then how do you get in there and put things in it?
The magic key word is "setappdata" and "getappdata".
As you guessed it, "setappdata" put things(matrix, array, strings, etc) in a specific place.

Let's try something simple now.
When it comes to MATLAB, object handle "0" is reserved for the entire monitor space.

Let's make a test matrix, x.

Now save it to the monitor screen's storage.

Go back to the storage and bring it out.

That's not bad.
I'll show you how to use this in the GUI programming next time.

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(' ')