Mat Lab Gui Tutorial
Mat Lab Gui Tutorial
Overview
The main difference between Matlab 6 and the previous versions is the
following. Matlab 6 uses java while the others used C++. I should mention that
knowledge of neither platform is necessary to use matlab6 properly.
Frame or ( figure)
Edit Box
Push Button
As I mentioned every object in the GUI will have an unique handle (name). This
handle will allow developers to access the objects properties. For instance let’s
say we wanted to change the text on the button from ‘Push Button’ to ‘Press
ME’. All we have to do is obtain the object’s handle. Once we have the handle
we can then set the ‘text’ property of the object to ‘Press ME’. Details regarding
actually doing this will be discuss shortly, in this section of the tutorial I have
deliberately decided to avoid code and focus on the overall process. The other
properties of the object ‘PushButton’ include things like ‘Background Color,
Enable, Font, FontAngle, FontName, Position, String, Style, Tag, Value, etc. A
full listing of it’s properties are given below. The programmer can change any of
these properties at design time.
Quick Re-Cap:
STRING PROPERTY
TAG PROPERTY
IN MATLAB 6
Matlab 6 has a function, which collects every handle in the GUI and places it in a
convenient data structure. This makes life very easy as the user does not have to
poll every object for it’s handle.
Example………
handles =
figure1: 102.0034
edit1: 3.0043
pushbutton1: 103.0039
To obtain a handle in the previous version the user must poll the object for it’s handle. To
poll the object the user must give every GUI object a unique ‘Tag’. For instance the
default tag for the ‘PushButton’ is ‘pustbutton1’. There for the following is what the user
would need to do in order to obtain the handle of the object.
To obtain handle for the remaining objects in the GUI the user must poll every object
individually for it’s handle. If there are many objects on the GUI this process becomes
laborious and tiresome.
For instance consider the GUI discussed above, if we wanted to copy whatever
was written on the button into the edit box we would need to do the following.
The we put this data into the edit box by using the
set function.
>> set(handles.edit1, ‘string’, var);
SUMMARY:
• Guihandle() -> This function obtains all the handles in the GUI.
• Get() -> Allows users to obtain an object for a single property at runtime.
• Set() -> Allows users to change an objects property at runtime.
EXERCISE 1: BUILDING A SIMPLE GUI IN MATLAB FROM SCRATCH.
From this point in the tutorial we will take the shortest route to developing a fully
functional GUI in Matlab. The previous section of the tutorial covered some of the
concepts Matlab uses to manage graphical objects. Starting from this section the tutorial
will become a lot more hands on.
Matlab is able to automatically generate a lot of code that is needed for GUI.
When working under time constraints this feature of Matlab comes in very handy. For
beginners, it’s ok to rely on the automatically generated code.
Matlab has a program called ‘guide’ which allows users to setup their GUI. The
guide tool is very intuitive to use and can be accessed from the command line by simply
typing in the following.
>> guide
STEP 1: In this step we will setup our GUI.
>> guide
If you have made a mistake in the GUI you can easily correct it at this point
without moving on. For instance, if you look closely in this GUI there is really no
way to select which of the two (sin(x) or cos(x) are to be plotted. We should have
used a different object in the first place.. perhaps a check box. We can easily
delete the wrong object and replace it with the one we want. Since this is our
first GUI we will keep it simple and get rid on one of the static text boxes.
Click on the Cos(x) box and hit the delete key and the cos(x) should disappear
from the GUI. Once that is done, save as ‘mygui’.
As soon as you save it Matlab should generate the skeleton source code for you
and the source code should automatically open in an editor.
Understanding the Skeletal Code
These are stubs where objects such as buttons, edit boxes get their bodies.
Step 8: Activating the buttons.
To run the program, simply go to the Matlab main window and call your program.
>> mygui