Unformatted text preview: Engineering 101 Engineering 101
Lecture 26 12/6/07
MATLAB I/O, some GUI info MiniQuiz Quote of the Day Quote of the Day
The more I read, the more I meditate, and the more I acquire, the more I am enabled to affirm that I know nothing. Voltaire Graphical User Interfaces Graphical User Interfaces MATLAB contains a variety of built in functions to help build graphical user interfaces (GUI). We will use a handful of these to make a GUI front end to our Mandelbrot set program. GUI Methods GUI Methods The function menu( titlestring, choices ) creates a menu with a title given by the titlestring and buttons containing the list of strings given by choices and returns the number of the one chosen. function mandelbrotgui stack = [1.8 1.8 1.3 1.3]; f = figure; res = 400; niter = 30; while 1 coords = stack(end,:); drawMandelbrot(coords, res, niter); choice = menu('Which one?', 'Zoom', 'Pan Out', 'Quit'); switch choice case 1 % Zoom case 2 % Pan Out case 3 % Quit break; end end mandelbrotgui mandelbrotgui GUI Methods GUI Methods The function waitforbuttonpress pauses and waits for the user to press the button. The function get(h, propertyname) returns the property named associated with the handle h. The handle gca refers to the currently active figure. The propertyname ‘CurrentPoint’ refers to the location in the figure. The function rbbox tracks a rubberband box. Case1: Zoom Case1: Zoom case 1 waitforbuttonpress; point1 = get(gca,'CurrentPoint'); % button down detected rbbox; point2 = get(gca,'CurrentPoint'); % button up detected stack(end+1,:)= [point1(1,1) point2(1,1) point1(1,2) point2(1,2)]; Case2: Pan Out Case2: Pan Out case 2 if size(stack,1) > 1 stack = stack(1:(end1),:); else midx = mean(stack(1,1:2)); midy = mean(stack(1,3:4)); lenx = abs(diff(stack(1,1:2))); leny = abs(diff(stack(1,3:4))); stack = [midx+lenx*[1,1], midy+leny*[1,1]]; end Input/Output in MATLAB Input/Output in MATLAB There are a number of ways you can handle input from keyboard and files as well as output to screen and files. Simple I/O: disp and input Formatted output: fprintf Storing data in files simply: save/load General file input/output: fopen, fscanf, fprintf and fclose. disp/input disp/input The most straightforward way to incorporate input/output into MATLAB programs is to use display and input. disp( data ) will output the data to the screen. Unlike display( ) which is called after any command that does not end with a semicolon, disp( ) does not put a “data =“ message before the output. disp() disp() Example: x = [5 : 9]; disp(x); Output: 5 6 7 8 9 disp() disp() Example: name = ‘Xena’; name2 = ‘: Warrior Princess’ disp([‘My name is ’ name name2]); Output: My name is Xena: Warrior Princess input() input() The easiest way to get input from the user is the input( ) command. The input(question) command prints the question and then returns the input read from the user via the keyboard. input() input() Example: num = input( ‘ How many? ‘); disp([ ‘ You want ‘ num ‘ ?’]); Output: How many? 10 You want 10 ? Which will output the following 3 rows of text?
1
00000000011111111112222222 00000000011111111112222222 12345678901234567890123456 12345678901234567890123456 abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
S = char(X) converts the array X that contains nonnegative integers representing character codes into a MATLAB character array. 2 3 4 Which will output the following 3 rows of text? 1
00000000011111111112222222 00000000011111111112222222 12345678901234567890123456 12345678901234567890123456 abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
S = char(X) converts the array X that contains nonnegative integers representing character codes into a MATLAB character array. 2 3 4 Formatted output Formatted output
Sometimes you want to specify the output precisely. The fprintf( ) command allows you to specify a formatting string. fprintf(format_string, variables) will print the values in the variables as specified in the format_string. Places for numbers in the format string are specified like: %12.5e %{+,,0}{field width}.{precision}{conversion} +: include sign, : left justify, 0: pad with zeros Formatted output Formatted output Conversion characters:
%c %d %e %E %f %s %u single character decimal number exponential notation (lowercase e) exponential notation (uppercase E) fixed point notation string decimal (unsigned) Formatted output Formatted output Special characters in the format string
\t \n \\ \% tab new line backslash percent fprintf() fprintf() Example: num = 1234.56789; fprintf(‘The number is %f !’,num); Output: The number is 1234.56789 ! fprintf() fprintf() Example: num = 1234.56789; fprintf(‘The number is %e !’,num); Output: The number is 1.2345678e+003 ! fprintf() fprintf() Example: num = 1234.56789; fprintf(‘The number is %.1f !’,num); Output: The number is 1234.6 ! fprintf() fprintf() Example: num = 1234.56789; fprintf(‘The number is %08.1f !’,num); Output: The number is 001234.6 ! Which program, given a list of inventory item names (prod) and a list of the quantity and price, will print a list in the format:
item hammer hammer nails nails saw saw quantity 20 2000 5 price %{+,,0}{field width}.{precision}{conversion} : left justify 12.50 %d decimal number 0.10 %f fixed point notation %s string 25.00
\n new line 1 2 3 Which program, given a list of inventory item names (prod) and a list of the quantity and price, will print a list in the format:
item hammer hammer nails nails saw saw quantity 20 2000 5 price %{+,,0}{field width}.{precision}{conversion} : left justify 12.50 %d decimal number 0.10 %f fixed point notation %s string 25.00
\n new line 1 2 3 News Flash! NASA preps for another launch 4:31pm Eastern Time today Carrying the Europeandeveloped Columbus laboratory http://www.nasa.gov/mission_pages/shuttle/main/index.html More News Flash! Robot ExoSkelton! Guitar Hero meets World of Warcraft i.e. Activision meets Vivendi’s Blizzard Entertainment Save/Load Save/Load Sometimes during a MATLAB session or in the course of executing an MFile program there are one or more matrices that have data you would like to save to use later possibly in a different context. save can be called with or without parentheses. If it is called with parentheses then each argument is a string. save save Examples save save( ) What it does Save all workspace variables in a file called matlab.mat save save Examples save mydata.mat save(‘mydata.mat’) What it does Save all workspace variables in a file called mydata.mat save save Examples save mydata.mat X Y save(‘mydata.mat’, ‘X’, ‘Y’) What it does Save the data in X and Y in a file called mydata.mat save save Examples save mydata.txt X Y ascii save(‘mydata.txt’, ‘X’, ‘Y’, ‘ascii’) What it does Save the data in X and Y in a file called mydata.txt in ASCII format. The default is a binary format that is not easily readable outside of MATLAB. save save Examples save mydata.txt X* ascii save(‘mydata.txt’, ‘X*’, ‘ascii’) What it does Save the data in any matrix starting with an X in mydata.txt in ASCII format. save save Examples save mydata.txt X* ascii double save(‘mydata.txt’, ‘X*’, ‘ascii double’) What it does Save the data in any matrix starting with an X in mydata.txt in 16 digit ASCII format. The default is 8 digits. save save CAVEATS Data must be 2D character or data array Imaginary part of complex numbers is lost If using load to read data all items must have the same number of columns All data is written as floating point numbers; there is no record of which were characters and which were floating points. Saving as ASCII loses all data names Which program will take in up to 4 string arguments and save the Which program will take in up to 4 string arguments and save the matrix with the name corresponding to each string in a .mat file with the same name? i.e. savevars(‘this’, ‘that’) would save the matrix this in the file this.mat and the matrix that in the file that.mat 1 2 Which program will take in up to 4 string arguments and save the Which program will take in up to 4 string arguments and save the matrix with the name corresponding to each string in a .mat file with the same name? i.e. savevars(‘this’, ‘that’) would save the matrix this in the file this.mat and the matrix that in the file that.mat 1 2 load load Examples load load( ) What it does Load all workspace variables from a file called matlab.mat load load Examples load mydata.mat load(‘mydata.mat’) What it does Load all workspace variables from a file called mydata.mat (Matlab assumes binary format) load load Examples load mydata.ext load(‘mydata.ext’) What it does Load all workspace variables from a file called mydata.ext (If file suffix is anything but .mat, then Matlab assumes ASCII format) load load Examples load mydata.ext mat load(‘mydata.ext’, ‘mat’) What it does Load all workspace variables from a file called mydata.ext that is in binary format. load load Examples load mydata.mat ascii load(‘mydata.mat’, ‘ascii’) What it does Load all workspace variables in a file called mydata.mat that is in ASCII format. load load Examples load mydata.mat X load(‘mydata.mat’, ‘X’) What it does Load the matrix X from a file called mydata.mat load load Examples load mydata.mat X Y load(‘mydata.mat’, ‘X’, ‘Y’) What it does Load the matrices X and Y from a file called mydata.mat load load Examples load mydata.mat X* load(‘mydata.mat’, ‘X*’) What it does Load the matrices that start with an X from a file called mydata.mat General File I/O General File I/O There are also several commands provided to write to files on disk. To open a file: fid = fopen( filename, permission ); fid = fopen(‘Myfile.txt’, ‘r’); To close a file: close(fid) Permissions Permissions ‘r’ ‘w’ ‘a’ ‘r+’ ‘w+’ ‘a+’ read only write to new file append to new or existing file read and write read and overwrite read and append fprintf with files fprintf with files You can use fprintf to output to files fid = fopen(‘Myfile.txt’, ‘w’); fprintf(fid, ‘It is %.2f meters long’, num); fscanf fscanf There is an analogous command called fscanf to get data fid = fopen(‘Myfile.txt’, ‘r’); decnum = fscanf(fid, ‘%d’); Which vectorizes this program? for a = 1:size(M,1) for b = 1:size(M,2) if( mod(a,2)==0 and mod(b,3)==0) M(a,b) = M(a1,b1); end end end Which vectorizes this program? for a = 1:size(M,1) for b = 1:size(M,2) if( mod(a,2)==0 and mod(b,3)==0) M(a,b) = M(a1,b1); end end end What is the result in N after executing What is the result in N after executing M = [ 1 5 0 1; 4 0 0 1]; N = M( :, [1 3] ) * M( :, [2 4] ); 14 20 0 1 2 5 1 3 1
20 4 4 0 40 4 0 5 1 What is the result in N after executing What is the result in N after executing M = [ 1 5 0 1; 4 0 0 1]; N = M( :, [1 3] ) * M( :, [2 4] ); 14 20 0 1 2 5 1 3 1
20 4 4 0 40 4 0 5 1 Which will create the matrix: Which will create the matrix: 1 0 0 0 2 1 2 0 0 0 1 0 2 0 2 1 Which will create the matrix: Which will create the matrix: 1 0 0 0 2 1 2 0 0 0 1 0 2 0 2 1 Which MATLAB function is equivalent to: Which MATLAB function is equivalent to:
void shuffle (vector <int> A, int max, vector <int> & B){ for(int i=0, B.clear( ); i< A.size( ); i++) if (A[ i ] <= max) B.push_back(A[ i ]); return; } Which MATLAB function is equivalent to: Which MATLAB function is equivalent to:
void shuffle (vector <int> A, int max, vector <int> & B){ for(int i=0, B.clear( ); i< A.size( ); i++) if (A[ i ] <= max) B.push_back(A[ i ]); return; } Which code will solve the following set of linear equations: 3x + 5y 2 = 0 x + 2y + 3z =0 x – 2z + 7 = 0 Which code will solve the following set of linear equations: 3x + 5y 2 = 0 x + 2y + 3z =0 x – 2z + 7 = 0 Next Lecture Next Lecture MLC – Phase 4 ...
View
Full Document
 Fall '07
 JeffRingenberg
 Volt, load load, workspace variables, Examples load mydata.mat

Click to edit the document details