Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 9 : Introduction to GUI & Revision Edward Cheung Room W311g.

Similar presentations


Presentation on theme: "The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 9 : Introduction to GUI & Revision Edward Cheung Room W311g."— Presentation transcript:

1 The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 9 : Introduction to GUI & Revision Edward Cheung email: icec@polyu.edu.hk Room W311g 2008m

2 2 Graphical User Interface GUI is the point of contact for human machine interface GUI incorporates objects such as Windows, icons, buttons menus and text Selecting or activating these objects causes the generation of an input event Application that provides GUI are more user friendly and easier to learn and use compare with command line interface (CLI) Simple GUI Output - Display Message Box  Example >>msgbox('Hello')

3 3 Default Dialog Boxes >>% a default warning box Warndlg Other default warning boxes are:- errordlg helpdlg warndlg

4 4 questdlg Create and display question dialog box Syntax:- button = questdlg('qstring','title','str1','str2','de fault') The dialog has three default buttons, Yes, No, and Cancel. >>answer=questdlg('Continue?'); Example: >> question='Do you want to continue?'; >> answer=questdlg(question,'Game Over','Yes','No','Help','Yes');

5 5 Cell arrays An array in which each element is a cell or bin Each cell can contain an array or any data type Group data sets that are related but with different dimensions Mathematical operations on cell array must be addressed to the contents of a cell Curly brackets {} is the cell array constructor and straight brackets [] is the standard array constructor Example:- Create & access a cell array with 4 elements:- >> A=[1 2 3;4 5 6;7 8 9]; B=2+5j; C=1:5; D=['A 2x2 Cell Array‘]; % example continue on next page

6 6 Example on Cell Array >> AA={A B; C D} % Form Cell Array with 4 elements AA = [3x3 double] [ 2.0000+ 5.0000i] [1x5 double] 'A 2x2 Cell Array' >> AA{1} % get first element of AA ans = 1 2 3 4 5 6 7 8 9 >> AB={AA pi} % Form Cell Array AB with AA & pi AB = {2x2 cell} [3.1416] >> AB{1}(1,1) % a mixture of brackets to address elements ans = [3x3 double] >> AB{1}{1,1} % get first element of AA ans = 1 2 3 4 5 6 7 8 9 % can use celldisp function to show entire array

7 7 Structure Array Use much like a database Similar to cell array, instead of using indexing methods as in other MatLab arrays, we can address the elements by field name Information in the structure array can be access using the array name, field name and the index number Stucture field names must begin with a letter and are case sensitive In MatLab, only Cell array and structure array can be used to manipulate a mixture of different data types

8 8 Structure Array Example:- Define a structure array using elements in last example. >> new_stru.matA=A; >> new_stru.strD=D; >> new_stru.complexB=B; >> new_stru.vectorC=C new_stru = matA: [3x3 double] strD: 'A 2x2 Cell Array' complexB: 2.0000+ 5.0000i vectorC: [1 2 3 4 5]

9 9 inputdlg Create input dialog box Answer=inputdlg(prompt,title,lineNo,DefAns) lineNo specifies the number of lines for each user entered value. lineNo can be a scalar, column vector, or matrix.  If lineNo is a scalar, it applies to all prompts.  If lineNo is a column vector, each element specifies the number of lines of input for a prompt.  If lineNo is a matrix, it should be size m-by-2, where m is the number of prompts on the dialog box. Each row refers to a prompt. The first column specifies the number of lines of input for a prompt. The second column specifies the width of the field in characters.

10 10 Example prompt={'Input function:','Input color:','Input Limit:'}; title='Plot function'; lines=1; defans={'sin(x)','r','2*pi'}; answers=inputdlg(prompt,title,lines,defans); x=0:0.1:str2num(answer{3}); y=eval(answers{1}); figure; plot(x,y,answer{2});

11 11 Other Dialog Box Functions >> lookfor 'dialog box' AXLIMDLG Axes limits dialog box. ERRORDLG Error dialog box. HELPDLG Help dialog box. INPUTDLG Input dialog box. LAYOUT script to define dialog box layout parameters. LISTDLG List selection dialog box. MATQDLG Workspace transfer dialog box. PAGEDLG Page position dialog box. PRINTDLG Print dialog box. QUESTDLG Question dialog box. TABDLG Create and manage tabbed dialog box. UIGETFILE Standard open file dialog box. UIGETPREF question dialog box with preference support UIPUTFILE Standard save file dialog box. UISETCOLOR Color selection dialog box. UISETFONT Font selection dialog box. WARNDLG Warning dialog box.

12 12 uicontrol creates a user interface control in the current figure window and returns a handle to it. h= uicontrol %default UI control get (h) % a list of properties of h get (h,'style') set (h,'style') % set the style of h uicontrol('PropertyName1',value1,'PropertyName2,' value2,...) Help uicontrol Help set

13 13 Callbacks Callbacks are statements that get executed in the command window when an UI element is activated Example: >> uicontrol ('string','Start plot', 'Callback', 'surf(membrane)') Callback control is a routine that executes whenever the uicontrol object has been activiated. The example creates a uicontrol (default style is pushbutton) and mark the button with text ‘Start Plot’. When this button is being pressed, the command surf(membrane) is executed and a plot will be generated in the workspace.

14 14 GUIDE Graphical User Interface Development Environment (GUIDE) is a toolset consists of a Layout Editor and tools that can be used to add and arrange objects in a “figure” window (untitled figure). The following tools can be accessed from the Layout Editor. Alignment Tool - align objects with respect to each other. Menu Editor - create window menus and context menus. Property Inspector - inspect and set property values. Object Browser - observe a hierarchical list of the Handle Graphics Component Palette – UI controls in the current MATLAB session. To start the Layout Editor New  File  GUI or enter >>guide at the command prompt

15 15 GUI files GUIDE stores the GUI in two files:- fig-file – a type of mat file contain GUI layout & components m-file – a function file contains initialization code & templates for callbacks that are needed to control GUI behaviour. You must add the callbacks to this file. GUIDE automatically opens this file in the editor when you save your GUI the first time. fig-file and the m-file must have the same name. Layout editor – place the components on UI and save in fig file

16 16 GUI m-file Structure Major sections of the GUI m-file are ordered as follows:- Comments Initialization  GUIDE initialization tasks, Do Not edit this code Opening function  Perform initialization tasks before user has access to UI Output function  Returns outputs to CLI Callbacks  Component and figure callbacks corresponding to events Utility or helper functions  Functions that are not associated with an event

17 17 Quiz Next Week 30% of total marks for this module 30 multiple choice questions 45 minutes Open notes and NO discussion with your neighbour A list of commands is given at the last few pages of the workbook that will be provided as usual On some commands that we may not covered in this training course, you should call help wisely. Other assessment:-  Workshop report, 1 page, submit to mailbox before 8-Dec  You can do this after the quiz  Keep a copy of the workshop report for logbook submission due 4 weeks after summer term.

18 18 The colon operator >>A=0:pi/4:pi A = 0 0.7854 1.5708 2.3562 3.1416 >> 100:-7:55 ans = 100 93 86 79 72 65 58 >> 55:7:100 ans = 55 62 69 76 83 90 97

19 19 Use End as a subscript >> a=[1:3;10:10:30;100:100:300] a = 1 2 3 10 20 30 100 200 300 >> a(end,end) ans = 300 >> a(1,end) ans = 3 >> a(end,1) ans = 100 >> a(end-1,end) ans = 30 >> a(end,end-1) ans = 200 >> a(end-5) ans = 2

20 20 Matrix manipulation & Transpose >> a(1:end-3) ans = 1 10 100 2 20 200 >> b=(a(1:end-3))' b = 1 10 100 2 20 200

21 21 Colon & matrix reduction % Make a into a row vector with colon >>a(:)' ans = 1 10 100 2 20 200 3 30 300 Examples on Matrix Reduction using colon  A(3,:)=[] % delete the third row in A  A(:,2:5)=[] % delete column 2 to 5 in A  A([1 4],:)=[] % delete 1 st & 4 th rows in A

22 22 Replicate a matrix >> a=[1 2 ; 3 4]; >>repmat(a,3,3) ans = 1 2 1 2 1 2 3 4 3 4 3 4 1 2 1 2 1 2 3 4 3 4 3 4 1 2 1 2 1 2 3 4 3 4 3 4 Useful matrix generators:- zeros, ones, rand, randn, eye

23 23 Transpose for complex number >> z=[1+2i,2+4i] z = 1.0000 + 2.0000i 2.0000 + 4.0000i >> z' ans = 1.0000 - 2.0000i 2.0000 - 4.0000i >> z.' ans = 1.0000 + 2.0000i 2.0000 + 4.0000i Elements are changed into its complex conjugate. Use.’ to avoid changing elements into its complex conjugate when transpose if needed

24 Example on nested For loop % Create a n=5 symmetrical matrix A(n,n) with element (k,m) equals to k/m for m>=k n=5; A=eye(n); for m=2:n for k=1:m-1 A(k,m)=k/m; A(m,k)=k/m; end 24 >>A = 1.0000 0.5000 0.3333 0.2500 0.2000 0.5000 1.0000 0.6667 0.5000 0.4000 0.3333 0.6667 1.0000 0.7500 0.6000 0.2500 0.5000 0.7500 1.0000 0.8000 0.2000 0.4000 0.6000 0.8000 1.0000

25 25 Interactive Curve Fitting % Example:- 2D interactive curve fitting % Figures -> Tools -> Basic Curve Fitting x=0:7 y=[0,11,44,58,77,85,95,97]; plot(x,y,'o') axis([0,7,0,100])


Download ppt "The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 9 : Introduction to GUI & Revision Edward Cheung Room W311g."

Similar presentations


Ads by Google