user interfaces in excel and matab
TRANSCRIPT
![Page 1: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/1.jpg)
User Interfaces in Excel and MATAB
Kipp MartinUniversity of Chicago
Booth School of Business
March 6, 2012
1
![Page 2: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/2.jpg)
Files
Excel Files:
I vanillaConeGUI.xlsm
I hotFudgeGUI.xlsm
MATLAB Files:
I vanillaConeGUI.m
I vanillaConeGUI.fig
I simulationDemoGUI.m
I simulationDemoGUI.fig
2
![Page 3: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/3.jpg)
Outline
GUI BasicsMotivation
VBA GUIsGUI ToolsGenerating the CodeOther Issues
MATLAB GUIs
3
![Page 4: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/4.jpg)
GUI Basics
Building a GUI in MATLAB is very similar to building one VBA (orany other IDE for that matter).
Key Ideas:
I There is a user interface (or user form) that the user interactswith in order to generate the model data
I There are “tools” that are part of a tool box used to constructthe user interface
I These tools generate stubs or callback functions.
I When you “click” on things this generate eventscorresponding to the callback functions. The code in thesefunctions is executed.
4
![Page 5: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/5.jpg)
Motivation
Why a GUI?
I Make it easy for the user! The user may not know aboutoptimization, simulation, or even much about Excel orMATLAB.
I Accuracy. By controlling the input into the model we havemore accurate data entry. Example – entering the State in aWeb form.
I Speed. It is much faster for the user to cycle through theGUI inputs than figure things out from scratch.
5
![Page 6: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/6.jpg)
VBA GUI Basics
A blank VBA user form. To get a UserForm go into the VBAeditor and select Insert → UserForm.
6
![Page 7: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/7.jpg)
VBA GUI Basics
The VBA ToolBox. See the “hammer and wrench” icon to bring upthe tool box. It is also available under View in the VBA Toolbar.
7
![Page 8: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/8.jpg)
VBA GUI Basics
A VBA user form with tools. (Drag them over from the Toolbox)
8
![Page 9: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/9.jpg)
VBA GUI Basics
Here is what the user will see.
9
![Page 10: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/10.jpg)
VBA GUI Basics
Here is the VBA property explorer.
10
![Page 11: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/11.jpg)
GUI Tools
I Command Button (VBA) Push Button (MATLAB):clicking on this will cause a procedure or function to execute.
I TextBox (VBA) Edit Text (MATLAB): used to enterparameter values or show values to the user – used for bothinput and output.
I Label (VBA) Static Text (MATLAB): Used to “explain”the other tools to the user. These cannot be used to provideany information to the code.
I ScrollBar (VBA) Slider (MATLAB): Can select values in acontinum between a min and max value.
I Image (VBA) Axes (MATLAB): Display a graphic.
11
![Page 12: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/12.jpg)
GUI Tools
I ListBox (VBA) Pop-Up Menu (MATLAB): select from afinite set of options.
I CheckBox (VBA) Check Box (MATLAB): select an optionif desired.
I OptionButton (VBA) Radio Button (MATLAB): selectamong several options
I Frame (VBA) Panel (MATLAB) organize a related set oftools or options
12
![Page 13: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/13.jpg)
GUI ToolsEach of the GUI tools has a Properties Window (Inspector).Here is one for VBA. Click on the icon in the User Form to bring itup.
13
![Page 14: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/14.jpg)
GUI ToolsHere is one for MATLAB. Click on the icon in the User Form tobring it up.
14
![Page 15: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/15.jpg)
Generating the Code
KEY IDEA ONE:For each tool there is an empty stub or callback Sub or function.VBA
Private Sub CommandButton1_Click()
End Sub
MATLAB
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1
% eventdata reserved - to be defined in a
%future version of MATLAB
% handles structure with handles and
%user data (see GUIDATA)
15
![Page 16: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/16.jpg)
Generating the Code
KEY IDEA TWO: You fill in the stub or callback with your owncode! VBA code for clicking on the Generate Histogram button.
numTrials = num_trials_scrollBar.Value
numBins = ListBox1.Value
ReDim values(numTrials)
Dim i As Long
For i = 1 To numTrials
values(i) = RandNorm(0, 1)
Next i
Dim chartName As String, fname As String
Call CreateHistogram("Histogram", numBins, values)
fname = ThisWorkbook.Path & "\temp.gif"
Worksheets("Histogram").ChartObjects(1).Chart.Export _
Filename:=fname, FilterName:="GIF"
Image1.Picture = LoadPicture(fname)
16
![Page 17: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/17.jpg)
Generating the Code
KEY IDEA TWO: You fill in the stub or callback with your owncode! MATLAB code for clicking on the Generate Histogrambutton.
function gen_Histogram_Callback(hObject, ...
eventdata, handles)
hist (randn(1, handles.num_trials), ...
handles.number_bins);
17
![Page 18: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/18.jpg)
Generating the Code
KEY IDEA THREE: The code is executed when you “click” or“change” a tool. This generates an event or callback.
VBA ScrollBar: the following code is generated when you movethe scroll bar.
Private Sub num_trials_scrollBar_Change()
slider_value.Text = num_trials_scrollBar.Value
End Sub
18
![Page 19: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/19.jpg)
Generating the Code
KEY IDEA THREE: The code is executed when you “click” or“change” a tool. This generates an event or callback.
MATLAB Slider: The following code is generated when you movethe slider.
function num_trials_slider_Callback(hObject, ...
eventdata, handles)
slider_value = get(hObject,’Value’)
handles.num_trials = floor( slider_value)
slider_value_Callback(hObject, eventdata, handles);
guidata(hObject,handles)
19
![Page 20: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/20.jpg)
Generating the Code
KEY IDEA FOUR: You must get “data” from the tools.
Here is how to get the value of the VBA ScrollBar(num trials scrollBar Change is the name of the VBAprocedure called on the event)
num_trials_scrollBar.Value
Here is how to get the value of the MATLAB Slider(num trials slider Callback is the name of the MATLABfunction called on the event)
slider_value = get(hObject,’Value’)
20
![Page 21: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/21.jpg)
Generating the Code
KEY IDEA FOUR: A second example.
Here is how to get the value of the ListBox(histogram listbox Click is the name of the VBA procedurecalled on the event)
Private Sub histogram_listbox_Click()
End Sub
Note that we don’t need to set any values in the procedure.Anywhere else in the code we can get the value using the statement
numBins = histogram_listbox.Value
21
![Page 22: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/22.jpg)
Generating the Code
KEY IDEA FOUR: A second example. Life is more complicatedin MATLAB.
function histogram_popup_Callback(hObject, eventdata, ...
handles)
val = get(hObject,’Value’)
str = get(hObject, ’String’)
switch str{val};
case ’20’
handles.number_bins = 20;
case ’50’
handles.number_bins = 50;
case ’100’
handles.number_bins = 100;
case ’1000’
handles.number_bins = 1000 ;
end
guidata(hObject, handles);22
![Page 23: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/23.jpg)
Generating the Code
KEY IDEA FOUR: A second example. Life is more complicatedin MATLAB.
To better understand the previous slide, here is MATLABgenerates for val and str.
val = 3
str = ’20’
’50’
’100’
’1000’
23
![Page 24: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/24.jpg)
Generating the Code
KEY IDEA FIVE: Transferring data between the functions orprocedures.
Life is much nicer with VBA. With VBA you can call the “value”of a procedure from anywhere in the User Form code. Forexample, in the Generate Histogram code we have:
numTrials = num_trials_scrollBar.Value
numBins = histogram_listbox.Value
24
![Page 25: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/25.jpg)
Generating the Code
KEY IDEA FIVE: Transferring data between the functions orprocedures.In MATLAB we put the data in a handler called handles. In thisexample we need the number of bins (numBins) and number of
trials (numTrials) to build the histogram.
Get the number of bins.
handles.number_bins = 1000 ;
Get the number of trials.
slider_value = get(hObject,’Value’)
handles.num_trials = floor( slider_value)
Pass the information to the other functions.
guidata(hObject,handles)
25
![Page 26: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/26.jpg)
Generating the Code
KEY IDEA FIVE: Transferring data between the functions orprocedures.In MATLAB we put the data in a handler called handles. In thisexample we need the number of bins (numBins) and number of
trials (numTrials) to build the histogram.
After putting the data in the handles we can use it
anywhere else. For example in the Generate Histogramfunction gen Histogram Callback we have
hist (randn(1, handles.num_trials), ...
handles.number_bins);
26
![Page 27: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/27.jpg)
Initialization
In VBA there is an initialization routine that calledUserForm Click() that gets called before the user sees the form.You can initialize data here. See my code where I initialize thevalues for the possible number of bins.
In VBA it is common to call the UserForm Click() from amain() procedure. This is done as simulationGUI.Show, wheresimulationGUI is the name of my User Form.
In MATLAB there is an initialization routine that calledsimulationDemoGUI OpeningFcn that gets called before the usersees the form. You can initialize data here. See my code where Iparameter values for the histogram.
27
![Page 28: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/28.jpg)
Start-up Worksheet
In VBA you can control the first worksheet the user sees and whichworksheets are visible. Here is my code.
Private Sub Workbook_Open()
Dim ws As Object
Worksheets("Begin").Activate
For Each ws In ActiveWorkbook.Sheets
If ws.Name <> "Begin" Then ws.Visible = False
Next
End Sub
28
![Page 29: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/29.jpg)
MATLAB GUI Basics
A blank MATLAB user form. The tool box is on the left. SelectFile and then New and then Figure to get this. There is also aToolbar icon.
29
![Page 30: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/30.jpg)
MATLAB GUI BasicsA MATLAB user form with tools. (Drag them over)
30
![Page 31: User Interfaces in Excel and MATAB](https://reader034.vdocument.in/reader034/viewer/2022051103/58a176561a28ab671b8c022e/html5/thumbnails/31.jpg)
MATLAB GUI Basics
Here is what the user will see.
31