Prof. Muhammad Saeed
Mathematical Modeling and Simulation
UsingMATLABMATLAB
Graphic User Interface (GUI)Basics
GUI Basics:1. uicontrols • Edit Text
• Static Text Label• Push Button• Radio Button• Button Group• Slider
• Check Box• Panel• List Box• Pop-up Menu• Toggle Button• Table• Axes• Menu• Toolbar
2Modeling & Simulations
3
Edit Textfunction edittext1_Callback(hObject, eventdata, handles)
s = get(hObject, 'String');set(hObject, ‘String’, s);% if number, use ‘num2str’ or ‘str2num’n = str2num(get(hObject,'string'));if isnan(n) errordlg(‘Enter a numeric value',’Input Error','modal'); uicontrol(hObject);
returnend
end Pushbutton
function pushbutton1_Callback(hObject, eventdata, handles)plot( ……… );…………..;end
Modeling & Simulations
4
Radiobuttonfunction radiobutton1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))do something
elsedo nothing
endset(handles.radiobutton1,'Value','Max‘)
endCheckboxfunction checkbox1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))%do something
else%do something else
end% can be used in a groupbox with other uicontrols
Modeling & Simulations
5Modeling & Simulations
6
Panel function uipanel1_ResizeFcn(hObject, eventdata, handles)
set(hObject,'Units','Points‘) % Was normalizedpanelSizePts = get(hObject,'Position'); % Now in pointspanelHeight = panelSizePts(4);set(hObject,'Units','normalized'); % Now normalized again% Keep fontsize in constant ratio to height of panelnewFontSize = 10 * panelHeight / 115; % Calculated in
pointsbuttons = get(hObject,'Children');set(buttons(1),'FontSize‘,newFontSize);% Resize the first
button% Do not resize the other button for comparison
end Listbox
function listbox1_Callback(hObject, eventdata, handles)index = get(hObject,'Value');list = get(hObject,'String');item = list{index}; set(handles.listbox1,'Value',2)
end
Modeling & Simulations
7
Popupmenu%Using value
function popupmenu1_Callback(hObject, eventdata, handles)val = get(hObject,'Value');switch val
case 1% code
case 2%code
endend
%Using String
function popupmenu1_Callback(hObject, eventdata, handles)val = get(hObject,'Value');list = get(hObject,'String');str = list{val}; % Convert from cell array to string
end
Modeling & Simulations
8Modeling & Simulations
Problem IPlanck’s Radiation Law is formulated as:
where ‘R’ is the spectral radiance and ‘T’ is temperature in Kelvins and ‘’ is wavelength of radiation
1) Simulate the Law by plotting ‘R’ vs ‘’ for different values of ‘T’.
‘’ range = 0.01m to 8m‘T’ range = 500K to 2500K and let user change the temperature ‘T’
2) Display temperature at each step.3) Display maximum Spectral Radiance ‘R’ at each step4) Display wavelength at at each ‘R’5) Draw a vertical line between ‘R’ and corresponding wavelength at each step.
9Modeling & Simulations
Problem II
T(C) -20 0 40 100 200 300 400 500 1000
x10-5 1.63 1.71 1.87 2.17 2.53 2.98 3.32 3.64 5.04
The above table gives the viscosity ( ) of air at different temperatures (T)Viscosity is expresed as:
‘C’ and ‘S’ are constantsDetermine ‘C’ and ‘S’ by curve-fitting using the above equation.
Plot a graph between ‘T’ and ‘’ for given data (only markers) and plot a line graph using the equation.Calculate the error.
10
END
Modeling & Simulations