180 likes | 326 Views
Lec 16 Chapter 10 Nov 2, 11 Chapter 10, user interface design examples. Events activating the key board >> a = waitforbuttonpress In response, Matlab displays a figure window.
E N D
Lec 16 Chapter 10 Nov 2, 11 • Chapter 10, user interface design examples
Events • activating the key board • >>a = waitforbuttonpress • In response, Matlab displays a figure window. • when you place the cursor inside the window and press down the mouse, the variable is set to 0 and the window is minimized. • instead, if you press a key, a is set to 1, and you can also extract that key using • >> get(gcf, ‘CurrentChar’)
Menus and other choices >> items = {‘Car’, ‘Truck’, ‘Motorcycle’, ‘delete’, ‘QUIT’}; >> menu (‘Next vehicle:’, items) Clicking on car returns 1, truck 2 etc. QUIT closes the menu.
The same functionality can be derived in other ways. >> questdlg(‘Does this make sense?’, ‘A question for you’) generates the following dialog box: Clicking on Yes (No) generates a string ‘Yes’ (‘No’).
Exercise: Create a menu containing 5 buttons ONE, TWO, THREE, FOUR and QUIT. So long as you click on the first four buttons, the clicked numbers are added, and when you press QUIT, the program outputs the total.
Character input >> textread('text1.txt', '%s', 'delimiter', ' '); >> foo = textread('text1.txt', '%s', 'delimiter', ' '); >> listdlg('liststring', foo) Now selecting a set of string from the box will create an array of selectd indices. >> listdlg('liststring', foo) ans = 1 4 5 8
Text Box inputs Here is a way to create a text box: Suppose the user enters the information as follows: Then, a becomes: a = 'Joe' '1/1/2010' '8'5''
A simple interactive program Write a program in Matlab that asks a user to guess an integer between 1 and N for some N, and repeatedly asks a sequence of questions of the form “Is your number greater than, equal to or less than X?” For various choices of X, and prints out the number.
For example: >> binarySearch(1,63) is your number less, equal or greater than32?g is your number less, equal or greater than48?g is your number less, equal or greater than56?l is your number less, equal or greater than52?l is your number less, equal or greater than50?e s = Your number is50 We can implement such a function using the input statement: reply = input('Do you want more? Y/N [Y]: ', 's'); if isempty(reply) reply = 'Y'; end
Warning: the code works fine for some values of such as n = 63, but may not work for other inputs, e.g. n = 64 The function is written as a recursive function.
function out = binarySearch(L,R) % interactively guesses a number by binary search if L==R s = strcat('Your number is ', L) out = L; else mid = floor((L+R)/2); reply = input(strcat('is your number less, equal or ... greater than ', num2str(mid), ‘ ?'), 's'); if isempty(reply) 'equal, less or greater are the options' out = binarySearch(L,R); elseif strcmpi(reply, 'equal') || strcmpi(reply, 'eq') || strcmpi(reply, 'e') s = strcat('Your number is ', num2str(mid)) out = mid; elseif strcmpi(reply, 'less') || strcmpi(reply, 'le') || strcmpi(reply, 'l') out = binarySearch(L, mid-1); elseif strcmpi(reply, 'greater') || strcmpi(reply, 'gr') || strcmpi(reply, 'g') out = binarySearch(mid+1, R); else 'equal, less or greater are the options' out = binarySearch(L,R); end; end;
Input from mouse You can use ginput function to get the position of mouse.
Two applications using ginput function Exercise 1: Write a function drawCircle(r) that draws a blank canvas on which a user can click on a point. The program draws a circle of radius r with the center at the clicked point.
Two applications using ginput function Exercise 2: Extending Exercise 1, write a function drawpoly(color) that draws a blank canvas. When a user clicks on a sequence of points, it draws the clicked points and waits until you click on a point close to the starting point. At this point, it connects all the points to create a polygon and colors the inside of the polygon with the specified color.
User interface design example* We want to design a calculator with only one function, say addition. The inputs are entered in text boxes and when the Add! Button is pressed, the sum is displayed on the screen. (*Thanks to Quan Quach for the tutorial and the web site.)
Creating components through UI We create the user interface through a “master” UI that has been implemented in Matlab. >> guide will give a template in which we can add various components and change their properties through property editor. We will walk through the tutorial in: http://blinkdagger.com/matlab/matlab-gui-graphical-user-interface-tutorial-for-beginners/
The video clip in the following link http://www.mathworks.com/products/matlab/demos.html Shows how to build a fairly complex GUI. We can learn by experimenting with it.