Input Output Input readsgets data for the program

  • Slides: 35
Download presentation
Input / Output Input – reads/gets data for the program Output – the product,

Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running) (RAM memory) or file I/O: (read/write to file) (Hard disk Memory) 1

Interactive Input Built-in functions for interactive input • • input menu keyboard (pause) 2

Interactive Input Built-in functions for interactive input • • input menu keyboard (pause) 2

input: input(‘string’) displays the text in string on the screen, and waits for the

input: input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input >> n = input('How many years? ' ) % prompts the user to input an integer number, % and saves it in n We will see on the command window: How many years? 3

input: input(‘string’) displays the text in string on the screen, and waits for the

input: input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input >> n = input('How many years? ' ) % prompts the user to input an integer number, % and saves it in n We will see on the command window: How many years? 12 n = 12 4

Input (cont. ): The input may also be a string >> more = input('More

Input (cont. ): The input may also be a string >> more = input('More Simulations ? (y/n): ' , 's' ) % Prompts the user to type "y” for % yes or “n” for no, and stores the % input as a string in “more”. The second argument, “s”, of the command directs MATLAB to save the user input as a string 5

Example >> more = 'y'; >> while (more == 'y') disp('eat') more = input('Are

Example >> more = 'y'; >> while (more == 'y') disp('eat') more = input('Are you hungry? ', 's') end eat y Are you hungry? more = y y eat Are you hungry? more = n >> 6

menu: menu(‘name’, ’option 1’, ’option 2’, . . ) creates an on-screen menu named

menu: menu(‘name’, ’option 1’, ’option 2’, . . ) creates an on-screen menu named as we requested in ‘name’, with several options The user selects any option using the mouse 7

Pop. Option=menu(‘Initial size', '200', '250', '300', '350') 8

Pop. Option=menu(‘Initial size', '200', '250', '300', '350') 8

Pop. Option=menu(‘Initial size', '200', '250', '300', '350') Pop. Option = 4 Pop. Initials=[200, 250,

Pop. Option=menu(‘Initial size', '200', '250', '300', '350') Pop. Option = 4 Pop. Initials=[200, 250, 300, 350]; Pop=Pop. Initials(Pop. Option) Pop= 350 9

Interactive Output We have seen some already by naming a variable, without ; it

Interactive Output We have seen some already by naming a variable, without ; it shows on output window: if we write A we get the whole matrix A= 1 2 3… if we write A(10, : ) we get ans= 10 20 30 40 50 60 70 80 90 100 If we don’t want to see the ans= , we use the function disp 10

disp(x) displays whatever is in the parentheses It can be anything - a string,

disp(x) displays whatever is in the parentheses It can be anything - a string, an integer, a real number, a vector, an array x= 'A string needs to be between single quotes' ; y = 4; z=6. 8; u =[2 3 4 6]; >> disp(x); disp(y); disp(z); disp(u) A string needs to be between single quotes 4 6. 8000 2 3 4 6 11

plot(x, y) Plots y as a function of x Often very useful to display

plot(x, y) Plots y as a function of x Often very useful to display results Example from your program Basics: Pop = 1. 0000 2. 0000 3. 0000 4. 0000 5. 0000 6. 0000 7. 0000 8. 0000 9. 0000 10. 0000 240. 0000 258. 0000 277. 3500 298. 1513 320. 5126 344. 5510 370. 3924 398. 1718 428. 0347 0 48. 0000 51. 6000 55. 4700 59. 6303 64. 1025 68. 9102 74. 0785 79. 6344 0 0 30. 0000 32. 2500 34. 6688 37. 2689 40. 0641 43. 0689 46. 2990 49. 7715 0 0 18. 0000 19. 3500 20. 8013 22. 3613 24. 0384 25. 8413 27. 7794 29. 8629 0 0 12

>>plot(Pop(: , 1), Pop(: , 2)) 13

>>plot(Pop(: , 1), Pop(: , 2)) 13

File Input/Output Reading-from and writing-to a file Save Load Writing and reading MATLAB data

File Input/Output Reading-from and writing-to a file Save Load Writing and reading MATLAB data fopen fclose fprintf fscanf frewind General text files 14

save(‘file. Name’) • Saves your workspace (all variables defined in the Command Window) in

save(‘file. Name’) • Saves your workspace (all variables defined in the Command Window) in a file named filename. mat • When called within a function, saves all the variables of defined within. load(‘file. Name’) • Restores the saved variables. ` 15

save('file. Name', var. Name) • Saves the variable “var. Name” in a file named

save('file. Name', var. Name) • Saves the variable “var. Name” in a file named filename. mat 16

fopen: Before we use a file (for reading or writing) - must be opened

fopen: Before we use a file (for reading or writing) - must be opened with this function The function returns an integer number, which is used as a file identifier so that other functions can access this file. 17

File handle File name Permission mode fid = fopen ('filename. txt', 'r') opens a

File handle File name Permission mode fid = fopen ('filename. txt', 'r') opens a file called filename. txt for reading of data Assigns an id-number to it in the background for us, the file is referred to as ‘fid’ Permission modes: • ‘r’ – read • ‘w’ – write (overwrite) • ‘a’ – write (append) 18

If the opening operation fails (e. g. , you ask to open for reading

If the opening operation fails (e. g. , you ask to open for reading a non-existing file), the function returns -1 as the value behind the handle, so you know there is problem f 1=fopen('input 6. txt', 'r') f 1 = -1 19 27

fprintf used to write formatted data into a file The structure of this function

fprintf used to write formatted data into a file The structure of this function is: fprintf(fid, FORMAT string, list of variables) fid: the file identifier, i. e. , the handle of the open file, on which we write 20

fprintf(fid, FORMAT string, list of variables) FORMAT string: a string (enclosed in ‘ ‘)

fprintf(fid, FORMAT string, list of variables) FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications Each specifier in this string is headed by the character “%” There a number or different specifiers- 21

fprintf(fid, FORMAT string, list of variables) FORMAT string: a string (enclosed in ‘ ‘)

fprintf(fid, FORMAT string, list of variables) FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications fid = fopen ('filename. txt', 'w'); fprintf(fid, 'The answer is %d not %fn', 42, 1. 5); Creates a new file with a single line The answer is 42 not 1. 500000 22

fprintf(fid, 'The answer is %d not %fn', 42, 1. 5); Specifier (integer) Conversion specification

fprintf(fid, 'The answer is %d not %fn', 42, 1. 5); Specifier (integer) Conversion specification (floating point) Special character (new line) 23

Any scalar can be written in different forms: a string, an integer, a real

Any scalar can be written in different forms: a string, an integer, a real number, a character etc. For example, the number 67 can be written as- 24

Specifier output meaning %d 67 (an integer) %10 d 67 (an integer shifted 10

Specifier output meaning %d 67 (an integer) %10 d 67 (an integer shifted 10 places to the right) %f 67. 000000 (a real number with 6 digits after the decimal point) %. 1 f 67. 0 (a real number with 1 digit after the decimal point) %. 3 f 67. 000 ( a real number with 3 digits after the decimal point) %10. 3 f 67. 000 ( a real number with 3 digits after the decimal point, and the whole number takes 10 spaces) %c C (a character , ASCII code of 67) %e 6. 700000 e+001 (a real number in scientific notation) Hence: fprintf(f 1, '%d 67 %5 d %f %. 1 f 67 67. 000000 67. 000 %. 3 f %8. 3 f ', 67, 67, 67) 67. 000 25

Writing vectors to file x = 0: . 2: 1 fid 1 = fopen('test.

Writing vectors to file x = 0: . 2: 1 fid 1 = fopen('test. txt', 'w'); fprintf(fid 1, '%6. 2 f n', x); Produces on the screen x = 0 0. 2000 0. 4000 0. 6000 0. 8000 1. 0000 But produces in a file named “test. txt” due to the n: 0. 00 0. 20 0. 40 0. 60 0. 80 1. 00 26

Writing an array to a file >> x = 0: . 2: 1 x

Writing an array to a file >> x = 0: . 2: 1 x = 0 0. 2000 0. 4000 0. 6000 0. 8000 1. 0000 >> y = [x ; exp(x)] y = 0 1. 0000 0. 2000 1. 2214 0. 4000 1. 4918 0. 6000 1. 8221 0. 8000 2. 2255 1. 0000 2. 7183 27

>> fprintf(fid, '%6. 2 f Writes to the file 0. 00 1. 000 0.

>> fprintf(fid, '%6. 2 f Writes to the file 0. 00 1. 000 0. 20 1. 221 0. 40 1. 492 0. 60 1. 822 0. 80 2. 226 1. 00 2. 718 %6. 3 fn', y'); 28

fclose A file on which we wrote must be closed to see its new

fclose A file on which we wrote must be closed to see its new contents A file from which we read must be closed and reopened (or rewind) to read again from its beginning fclose(fid) closes the file associated with the identifier “fid”. The function returns 0 if successful and -1 if not. fclose('all') % closes all open files 29

Example: %Temptable- generates and writes a temperature % table script file to generate a

Example: %Temptable- generates and writes a temperature % table script file to generate a Fahrenheit-Celsius % temperature table. The table is written in a file % named ‘Temperature. table’ F=-40: 5: 100 C=(F-32)*5/9 t=[F; C]; fid=fopen('Temperature. table', 'w'); fprintf(fid, ' Temperature Tablen'); fprintf(fid, ' ________n'); fprintf(fid, ' Fahrenheit Celsiusn'); fprintf(fid, ' %4 i %15. 2 fn', t) fclose(fid); 30

When we open the file “Temperature. table” we find Temperature Table ________ Fahrenheit Celsius

When we open the file “Temperature. table” we find Temperature Table ________ Fahrenheit Celsius -40. 00 -35 -37. 22 -30 -34. 44 -25 -31. 67 -20 -28. 89 -15 -26. 11 -10 -23. 33 -5 -20. 56 0 -17. 78 5 -15. 00 10 -12. 22 15 -9. 44 20 -6. 67 25 -3. 89 30 -1. 11 35 1. 67 40 4. 44 45 7. 22 50 10. 00 55 12. 78 60 15. 56 65 18. 33 70 21. 11 75 23. 89 80 26. 67 85 29. 44 90 32. 22 95 35. 00. 31

fscanf A = fscanf(fid, FORMAT, SIZE) Reads into A formatted data from file “fid”

fscanf A = fscanf(fid, FORMAT, SIZE) Reads into A formatted data from file “fid” Converts it according to FORMAT string The format string is recycled through the file until an end-of-file is reached or the amount of data specified by SIZE is read in. 32

Example – Let’s open and read from file we recently wrote on: x =

Example – Let’s open and read from file we recently wrote on: x = 0: . 2: 1; y = [x; exp(x)]; % Here fid = fopen('exp. txt', 'w'); fprintf(fid, '%6. 2 f %6. 3 fn', y); fclose(fid); fid=fopen('exp. txt', 'r') we created it % open it again for reading % read from it A=fscanf(fid, '%f %f', [2, inf]) % 2 rows only , whole length % inf can be used only in the second dimension A= 0 0. 2000 0. 4000 0. 6000 0. 8000 1. 0000 1. 2210 1. 4920 1. 8220 2. 2260 2. 7180 33

fclose(fid); % close and open the file again fid=fopen('exp. txt', 'r') % Now asking

fclose(fid); % close and open the file again fid=fopen('exp. txt', 'r') % Now asking just for 2 rows and 3 columns A=fscanf(fid, '%f %f', [2, 3]) A = 0 1. 0000 0. 2000 1. 2210 0. 4000 1. 4920 34

frewind(fid) Sets the file position indicator to the beginning of the file “fid” Useful

frewind(fid) Sets the file position indicator to the beginning of the file “fid” Useful if we need to read same file again Replaces closing and opening again 35