Monday 20 April 2009

Simple GUI with MATLAB® pragmatically programming

You must now what is a GUI before trying this stuff. A graphical user interface (GUI) is a graphical display that contains devices, or components, that enable a user to perform interactive tasks. To perform these tasks, the user of the GUI does not have to create a script or type commands at the command line. Often, the user does not have to know the details of the task at hand.

The GUI components can be menus, toolbars, push buttons, radio buttons, list boxes, and sliders, just to name a few. In MATLAB® software, a GUI can also display data in tabular form or as plots, and can group related components.

The following figure illustrates a simple GUI.















The GUI contains
-A push button component
-An edit button component

When you click a push button, A string "Hello World" will be showed in edit box.


How Does a GUI Work?
Each component, and the GUI itself, is associated with one or more user-written routines known as callbacks. The execution of each callback is triggered by a particular user action such as a button push, mouse click, selection of a menu item, or the cursor passing over a component. You, as the creator of the GUI, provide these callbacks.

So what Is a Callback?
A callback is a function that you write and associate with a specific component in the GUI or with the GUI figure itself. The callbacks control GUI or component behavior by performing some action in response to an event for its component. The event can be a mouse click on a push button, menu selection, key press, etc.

Okay, too much talking (too long so boring, I too exhausted to write this hahaha :) ). Here the source code listing:



function simple_gui
% Simple GUI with MATLAB to show how it works

% Initialize and hide the GUI as it is being constructed
fh = figure('Visible', 'off', 'Position', [360, 400, 300, 180]);

%Construct the components
% Push Button
hButton = uicontrol(fh, 'Style', 'pushbutton',...
'String', 'Push Button',...
'Position', [20, 120, 70, 25],...
'Callback',{@hButton_Callback});
% Edit Button
hEdit = uicontrol(fh, 'Style', 'edit',...
'String', '',...
'Position', [100 115 130 35]);

% Make the GUI visible.
set(fh,'Visible','on');

% Callback for hButton to show string 'Hello World' in edit Button
function hButton_Callback(source, eventdata)
set(hEdit, 'String', 'Hello World');
end

end







Well, i will explain it shortly,


function simple_gui
.
.
.
end
Every MATLAB® script with callback should be started by 'function' and ended with 'end' because this script is written using nested functions.

% blablabla
Every statement starting with '%' is a comment and will not executed by MATLAB®.


fh = figure('Visible', 'off', 'Position', [360, 400, 300, 180]);
This statement ini create figure object called fh, not visible to user. A GUI is a figure object. The Position property is a four-element vector that specifies the location of the GUI on the screen and its size: [distance from left, distance from bottom, width, height]. Default units are pixels.


hButton = uicontrol(fh, 'Style', 'pushbutton',...
'String', 'Push Button',...
'Position', [20, 120, 70, 25],...
'Callback',{@hButton_Callback});
This statement create a push button user interface control objects hButton, With fh as its GUI parent, has a String 'Push Button' on it, locate in a position 20 from left of screen, 120 from bottom screen, 70 width, 25 height, also has a callback function 'hButton_Callback' in response with event on this object.


hEdit = uicontrol(fh, 'Style', 'edit',...
'String', '',...
'Position', [100 115 130 35]);
Same explanation with hButton, but the object is an edit box.

set(fh,'Visible','on');
Setting property fh so it will be shown in screen with its components (a push button and edit box).


function hButton_Callback(source, eventdata)
set(hEdit, 'String', 'Hello World');
end
This part is a callback to hButton object. When the push button is pressed it will trigger edit box to show 'Hello World' on it.


Save the source code in a text file with extension M or m, then run it from MATLAB® command line.















Reference:
1. MATLAB® R2008A help file.
2. My fresh brain :).



Have a nice Try,


Juan Rio Sipayung

No comments: