4 matlab training

19
4. MATLAB Dr. Kashif Mahmood Rajpoot SEECS, NUST Islamabad [email protected] NUST Science Society (NSS) Workshop

Transcript of 4 matlab training

Page 1: 4   matlab training

4. MATLAB Dr. Kashif Mahmood Rajpoot

SEECS, NUST Islamabad [email protected]

NUST Science Society (NSS) Workshop

Page 2: 4   matlab training

Workshop Contents: Session 4

• GUI: Controls, static element, axes, menus, callbacks, hObject, eventdata, handles, figures, GUIDE, Property Inspector, guidata, dialog boxes

• Simulink

• Conclusion

Page 3: 4   matlab training

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

Page 4: 4   matlab training

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

Page 5: 4   matlab training
Page 6: 4   matlab training
Page 7: 4   matlab training

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

Page 8: 4   matlab training
Page 9: 4   matlab training

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.

Page 10: 4   matlab training

MyFirstGUI

• Create a GUI that inputs two numbers and shows the result of their addition

Page 11: 4   matlab training

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

Page 12: 4   matlab training

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

Page 13: 4   matlab training
Page 14: 4   matlab training

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’

Page 15: 4   matlab training

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

Page 16: 4   matlab training

Practice GUI

Page 17: 4   matlab training

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

Page 18: 4   matlab training

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

Page 19: 4   matlab training

Conclusion

• MATLAB is a wonderful and easy-to-use environment

– Scientific computations

– Visualization (i.e. plotting)

– Prototyping

• MATLAB Central File Exchange