Sunday, February 26, 2012

GUI in MATLAB 5

Today's portion may look like a big jump from last time, but most of it is same as the last post.
If you've been wondering what this figure handles, axes handles, etc. were for, today is the day you'll see what they can do.

For this post I wrote a short function. This website doesn't let me attach the file directly, so I copy-pasted the code at the bottom of this post.

So, here we go.

First it starts with declaring function. Today's portion won't work as a script m file. It needs to be a function, because it's housing another LOCAL function inside.
Let's call the main function GUI. What a name.
Then, the usual stuff follows.
Make x-array, 2 y-arrays, and open a figure, put axes on, make 2 plots with different color, and limit the axis.
Not so different from last time

Ok. Now, add 2 buttons using uicontrol. Yes, that's right. "uicontrol" is the magic command making buttons (actually it's more than just a button, but we'll play with them next time). The idea is same as figures or axes.
There's position and background color. I didn't put "Parent" because we're dealing with just 1 figure. If you have more than one figure opened, you may want to specify that here.
Then there's this new thing. "callback"
"callback" decides what will happen if the button is activated. In this case it calls a local function (it's local because the function is sitting in a same file) called "LOCALButton".

When it calls the "LOCALButton" it sends out the plot handle with it (P1 and P2). We'll examine "callback" in more detail some other time.
Oh. It's uicontrol. That's the magic word.

So, when the button is pressed, it calls LOCALButton and send out plot handle to the local function.
This is what the local function looks like.
The input is varargin, meaning variable argument input (or something like that). This is used when it's unclear what kind of input is coming in.
The varargin coming in is in cell array format. So you need to use {} to retrieve the information.
If you want to know what else is in there, add "varargin" there (can you guess the first number there?)
This is the local function LOCALButton

So, what this local function does is changing on/off of the plot by changing its visibility.
Perhaps it was a bit too much for one post, but I had to cover it all to explain it.


If you're still confused, don't worry. I'll do the similar stuff again with different uicontrol types next time.


Which button do you wanna press? Blue or Red?



Below is the source code. Select-Copy-Paste-Save as GUI.m

% GUI Sample
function GUI
x = 1:10;       % Generate arbitrary data
y1= rand(1,10);
y2= rand(1,10);

% Open a window.
F1 = figure('Position',[50 200 700 400],'toolbar','none','menubar','none');
% Put a drawing board on it.
A1 = axes('Parent',F1,'unit','pixel','Position',[50 50 400 320]);

% Plot the data in different colors
P1 = plot(x,y1,'b','Parent',A1);
hold on
P2 = plot(x,y2,'r','Parent',A1);
hold off
xlim([1 10])
ylim([0 1])

% Make buttons. One in blue and the other in red.
% 'callback' determined what will happen when the button is pressed.
% In this case it'll call another function called LOCALButton.
% When it calls the LOCALButton, it'll send out either P1 or P2.
B1 = uicontrol('Position',[500 250 100 100],'BackgroundColor',[0 0 1],...
    'callback', {@LOCALButton P1});
B2 = uicontrol('Position',[500 100 100 100],'BackgroundColor',[1 0 0],...
    'callback', {@LOCALButton P2});

% This is the LOCALButton fuction.
% It's pretty common to assign varargin for the input when it is unknown
% what kind of input is coming.
% varargin is in cell array format.
function LOCALButton(varargin)
% Whatever was sent from above, 3rd cell is the input.
% So the plot handle from above is varargin{3}.
CrntP = varargin{3};

% Here I can't say, get(CrntP,'visible') == 'on'
% because if it's 'off', 'on'=='off' will give you an error.
% Try that in the command window.
if length(get(CrntP,'visible')) == 2
    set(CrntP,'visible','off')
else
    set(CrntP,'visible','on')
end






No comments:

Post a Comment