150 likes | 294 Views
Lecture (7). Introduction to GUI Eng. Osama Talaat. Predefined Dialogs. Predefined Dialogs. Predefined Dialogs. Predefined Dialogs. Predefined Dialogs. Menu :. Predefined Dialogs. clear all ; close all ; clc ; waitbar (0/10 , 'Please wait' ); for k = 1:10
E N D
Lecture (7) Introduction to GUI Eng. Osama Talaat
Predefined Dialogs • Menu:
Predefined Dialogs clear all; close all; clc; waitbar(0/10,'Please wait'); for k = 1:10 pause(1) % computations here waitbar(k/10); end
Customize New GUI • Create new folder for each new GUI in the current directory and enter it. • Open the GUI Design Environment (Editor): >> guide
Length Converter • Insert the components from the left vertical panel. • Arrange them as you like. • Double Click any component to change its properties such as string and name. • Save and run.
String & Tag • Change the tag of each element to a meaningful name to be used in programming. • convert_button, m_edit, cm_edit, mm_edit, in_edit.
Button Callback • Program the event of button click. • Right click it >> view callbacks >> callback. • The structure handles is used to handle any element: • handles.convert_button, handles.m_edit, handles.cm_edit, handles.mm_edit, handles.in_edit • To get any element property use this function: m = get(handles.m_edit,'string'); m = str2num(get(handles.m_edit,'string')); • To set any element property use this function: set(handles.mm_edit,'string',num2str(m*1000))
Button Callback % --- Executes on button press in convert_button. functionconvert_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))
Several Source Units • Resize the window to contain the new elemnts. • Add another edit and static for the input and rearrange the layout to bring the meter into bottom. • Add a pop-up menu, with tag: unit_menu. • String it with the units in separate lines. • The index of the selected item is stored in the property ‘value’ of the pop-up menu.
Button Callback unit=get(handles.unit_menu,'value'); in=str2num(get(handles.input_edit,'string')); ifunit==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
BEST WISHES Eng. Osama Talaat