You are on page 1of 15

1

Lecture (7)
Introduction to G U I
Eng.O sam a Talaat
2 Predefined Dialogs
H elp D ialog W arning D ialog Error D ialog
3 Predefined Dialogs
M essage Box
4 Predefined Dialogs
Input D ialog:
5 Predefined Dialogs
Q uestion D ialog:
6 Predefined Dialogs
M enu:
7 Predefined Dialogs
W ait Bar:

clear all; close all; clc;

waitbar(0/10,'Please wait');
for k = 1:10
pause(1) % computations here
waitbar(k/10);
end
8 Customize New GUI
Create new folder for each new G U Iin the current
directory and enter it.
O pen the G U ID esign Environm ent (Editor):
>> guide
9 Length Converter
Insert the com ponents from the left verticalpanel.
Arrange them as you like.
D ouble Click any com ponent to change its
properties such as string and nam e.
Save and run.
10 String & Tag
Change the tag of each elem ent to a m eaningful
nam e to be used in program m ing.
convert_button,m _edit,cm _edit,m m _edit,in_edit.
11 Button Callback
Program the event of button click.
Right click it > > view callbacks > > callback.
The structure handles is used to handle any elem ent:
handles.convert_button,handles.m _edit,handles.cm _edit,
handles.m m _edit,handles.in_edit
To get any elem ent property use this function:
m = get(handles.m_edit,'string');
m = str2num(get(handles.m_edit,'string'));
To set any elem ent property use this function:
set(handles.mm_edit,'string',num2str(m*1000))
12 Button Callback
% --- Executes on button press in
convert_button.
function convert_button_Callback(hObject,
eventdata, handles)
% hObject handle to convert_button (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
m=str2num(get(handles.m_edit,'string'));
set(handles.mm_edit,'string',num2str(m*1000))
set(handles.cm_edit,'string',num2str(m*100))
set(handles.in_edit,'string',num2str(m/0.0254))
13 Several Source Units
Resize the w indow to contain the new elem nts.
Add another edit and static for the input and
rearrange the layout to bring the m eter into bottom .
Add a pop-up m enu, w ith tag: unit_m enu.
String it w ith the units in separate lines.
The index of the selected item is stored in the
property value of the pop-up m enu.
unit=get(handles.unit_menu,'value');
Button
in=str2num(get(handles.input_edit,'string'));
C allback
14 if unit==1 %meter
set(handles.m_edit,'string',num2str(in))
set(handles.mm_edit,'string',num2str(in*1000))
set(handles.cm_edit,'string',num2str(in*100))
set(handles.in_edit,'string',num2str(in/0.0254))
elseif unit==2 %millimeter
set(handles.m_edit,'string',num2str(in/1000))
set(handles.mm_edit,'string',num2str(in))
set(handles.cm_edit,'string',num2str(in/10))
set(handles.in_edit,'string',num2str(in/25.4))
elseif unit==3 %centimeter
set(handles.m_edit,'string',num2str(in/100))
set(handles.mm_edit,'string',num2str(in*10))
set(handles.cm_edit,'string',num2str(in))
set(handles.in_edit,'string',num2str(in/2.54))
elseif unit==4 %inch
set(handles.m_edit,'string',num2str(in*0.0254))
set(handles.mm_edit,'string',num2str(in*25.4))
set(handles.cm_edit,'string',num2str(in*2.54))
set(handles.in_edit,'string',num2str(in))
end
15
BEST WISHES
Eng. O sam a Talaat

You might also like