View
199
Download
3
Category
Preview:
Citation preview
4. MATLAB Dr. Kashif Mahmood Rajpoot
SEECS, NUST Islamabad kashif.rajpoot@seecs.edu.pk
NUST Science Society (NSS) Workshop
Workshop Contents: Session 4
• GUI: Controls, static element, axes, menus, callbacks, hObject, eventdata, handles, figures, GUIDE, Property Inspector, guidata, dialog boxes
• Simulink
• Conclusion
GUI
• GUI – a pictorial interface to a program
– Make programs easier to use with intuitive controls (buttons, text, lists, menus, etc)
– Easy interaction with mouse
GUI – Principal elements • GUI Components
– Controls (push buttons, edit boxes, lists, etc.)
– Static elements (frames, text strings)
– Menus
– Axes
• Figures – GUI components are arranged within a Figure which is a window on
computer screen
• Callbacks – A way to respond to any user interaction (mouse or keyboard input) or
event
– Code executed in response to an event is known as callback
– There must be a callback to implement the function of each GUI component
Creating and displaying a GUI
• MATLAB GUIs are created using a tool called guide, the GUI Development Environment
– guide allows the programmer to layout GUI components
• Once the GUI components are in place, the programmer can edit their properties (name, size, color, text, etc.)
• Upon saving, guide creates a working MATLAB program that programmer can modify to implement the behavior of GUI
Creating and displaying a GUI 1. Decide the elements required for GUI and the
function of each element. Make a rough layout. 2. Use MATLAB tool guide to lay out the
components on a Figure. 3. Use the Property Inspector to give each
component a unique name (‘tag’) and set other characteristics (color, text, etc.)
4. Save. This creates 2 files: .fig (containing the actual GUI) and .m (containing the code to load the figure and callbacks)
5. Write code for implementing the action behavior associated with each callback function.
MyFirstGUI
• Create a GUI that inputs two numbers and shows the result of their addition
Structure of callback subfunction function MyFirstButton_Callback(hObject,
eventdata, handles)
hObject – handle to parent figure
eventdata – reserved variable, not for our use
handles – a structure containing the handles of all GUI components on the figure and any user-defined application data
• Note that each callback function has access to handles structures and can modify any GUI component
Dialog boxes
• Dialog box – a special type of figure used to display information or to get input from user
– Modal – does not allow other windows to be accessed until it is dismissed. Typically used for error messages which need urgent attention.
– Non-modal - does not block access to other windows
Menus
• Menu – allows a user to select actions from a list of choices without additional components appearing on the GUI
• MATLAB menus are created using Menu Editor
• Suppressing the default menu
– Set ‘MenuBar’ property of Figure to ‘none’
MATLAB GUI
• GUI components are created using the guide tool
• The GUI components can be set by setting the characteristics in the Property Inspector
• The callback functions are written in the m-file to implement the action behavior of GUI components
Practice GUI
Simulink
• Simulating a dynamic system
• Simulink is a MATLAB add-on that facilitates
– Simulation by graphical building-blocks, without knowing any bit of MATLAB programming
Simulink
• Sources – Originator of data (e.g. file, workspace, etc)
• Sinks – Terminator of data (e.g. file, display screen)
• Image processing example – Thresholding
– Edge detection
– Rotation
Conclusion
• MATLAB is a wonderful and easy-to-use environment
– Scientific computations
– Visualization (i.e. plotting)
– Prototyping
• MATLAB Central File Exchange
Recommended