



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: Computation for the Sciences; Subject: Computer Science; University: Wellesley College; Term: Spring 2010;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
Department of Computer Science Wellesley College
Interactive Programs
Graphical User Interfaces
GUIs 13-
Structures
A structure can store multiple values of different types
gold. name = 'gold'; gold. type = 'metal'; gold. symbol = 'Au'; gold. atomNum = 79; gold. mbPoints = [1064 2856]; gold. bohrmodel = goldPict;
name 'gold' type 'metal'
symbol 'Au' atomNum (^79) mbPoints
bohrModel
1064 2856
structure name
field name
field value
GUIs 13-
Structures make sharing easy
function describeElement (element) % shows the properties stored in the input element structure
disp(['name of element: ' element.name]); disp(['type of element: ' element.type]); disp(['atomic symbol: ' element.symbol]); disp(['atomic number: ' num2str(element.atomNum)]); disp(['melting point: ' num2str(element.mbPoints(1)) ... ' degrees Celcius' ]); disp(['boiling point: ' num2str(element.mbPoints(2)) ... ' degrees Celcius']); imshow(element.bohrModel);
GUIs 13-
Sharing structures
describeElement(gold) name of element: gold type of element: metal atomic symbol: Au atomic number: 79 melting point: 1064 degrees Celcius boiling point: 2856 degrees Celcius
name 'gold' type 'metal' symbol 'Au' atomNum 79 mbPoints
bohrModel
1064 2856
GUIs 13-
Properties of graphics objects
All plotting and graphics functions create graphic objects
Each graphics object is identified by a unique number call a handle that can be assigned to a variable: p1=plot([0 1 2], [2 1 3]);
Graphics objects have properties that control their appearance on the screen and can be viewed with the Property Inspector: inspect(p1)
GUIs 13-
Accessing properties using MATLAB code
Graphics object properties can be accessed with the get function: get(object, property)
For example,
get(p1, 'LineWidth');
GUIs 13-
Graphics object properties may be set* by
... editing the value in the Property Inspector window
... specifying the property name and value when creating the graphics object: p1 = plot([0 1 2], [2 1 3], … 'LineWidth', 1);
... using the set function: set(object, property, value)
set(p1, 'LineWidth', 1);
Subfunctions
An M-file can only contain one function that can be called from the Command Window or from another code file
This function must be placed at the beginning of the file and its name must be the same as the file name
Othersubfunctions can be defined in an M-File, but can only be called by functions in the same M-File
GUIs 13-
Functions defined in energyGUI.m
energyGUI: top-level function at the beginning of the file that is called from the Command Window. This function initializes the GUI program and opens the GUI window.We will not modify this function
energyGUI_OpeningFcn: executed just before the GUI window is made visible. We will modify this function to set up data for the program
energyGUI_OutputFcn: returns outputs to the Command Window.We will not modify this function
GUIs 13-
Callback functions
For each component, the header of a Callback function is created. These functions are invoked automatically when the user interacts with the corresponding component
Invokes closeButton_Callback when clicked by user
GUIs 13-
Inputs to GUI functions
hObject is a number, thegraphics handle, that uniquely identifies the GUI component and its associated properties eventdata is not used in the current version of MATLAB handles is a structure that contains information that needs to be shared between functions in this file. Initially it contains a field for each GUI component created, using Tag property as name: handles. axes handles. sourceMenu handles. sourceToggle handles. titleLabel handles. titleBox handles. widthCheckbox handles. plotButton handles. closeButton handles. figure
Value of each field is the graphics handle for that component
GUIs 13-
Adding actions
function energyGUI_OpeningFcn (hObject, eventdata, handles, varargin) % setup data to use for plotting [handles.years handles.produce handles.consume] = setupEnergy;
function sourceToggle_Callback (hObject, eventdata, handles) % use state of toggle button to set text label on button if (get(hObject, 'Value') == 0) set(hObject, 'String', 'produce'); else set(hObject, 'String' , 'consume'); end guidata(hObject, handles);
copy changes to global handles structure
GUIs 13-
More action
function plotButton_Callback (hObject, eventdata, handles) % setup data source requested by user from state of toggle button if (get(handles.sourceToggle, 'Value') == 0) dataSource = handles.produce; else dataSource = handles.consume; end % get index of selected energy source sourceIndex = get(handles.sourceMenu, 'Value'); % use state of checkbox to determine line width linewidth = get(handles.widthCheckbox, 'Value') + 1; % plot the data with the requested properties plot(handles.years, dataSource(sourceIndex, :), 'Linewidth’, linewidth); xlabel('years') ylabel('quadrillion btu') title(get(handles.titleBox, 'String')) GUIs 13-
Time for you to leave
function closeButton_Callback (hObject, eventdata, handles) % close GUI figure window delete(handles.figure1);