1 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)
2 Interactive Input Built-in functions for interactive input input menu keyboard (pause)
3 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?
4 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
5 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
6 >> more = 'y'; >> while (more == 'y') disp('eat') more = input('Are you hungry?','s') end eat Are you hungry? more = y eat Are you hungry? more = y eat Are you hungry? more = y eat Are you hungry? more = n >> y y y y Example
7 menu: menu(‘name’,’option1’,’option2’,..) creates an on-screen menu named as we requested in ‘name’, with several options The user selects any option using the mouse
8 PopOption=menu(‘Initial size','200', '250','300','350')
9 PopInitials=[ 200, 250, 300, 350]; Pop=PopInitials(PopOption) Pop= 350 PopOption=menu(‘Initial size','200', '250','300','350') PopOption = 4
10 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= If we don’t want to see the ans=, we use the function disp
11 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 =[ ]; >> disp(x); disp(y); disp(z); disp(u) A string needs to be between single quotes
12 plot(x,y) Plots y as a function of x Often very useful to display results Example from your program Basics: Pop =
13 >>plot(Pop(:,1),Pop(:,2))
14 File Input/Output Reading-from and writing-to a file Save Load fopen fclose fprintf fscanf frewind Writing and reading MATLAB data General text files
15 save(‘fileName’) 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(‘fileName’) Restores the saved variables. `
16 save('fileName',varName) Saves the variable “varName” in a file named filename.mat
17 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.
18 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’ File handleFile namePermission mode Permission modes: ‘r’ – read ‘w’ – write (overwrite) ‘a’ – write (append)
19 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 f1=fopen('input6.txt','r') f1 = 27
20 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
21 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 are a number or different specifiers-
22 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 %f\n',42,1.5); Creates a new file with a single line The answer is 42 not
23 fprintf(fid,'The answer is %d not %f\n',42,1.5); Specifier (integer)Conversion specification (floating point) Special character (new line)
24 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-
25 Specifier output meaning %d67 (an integer) %10d 67 (an integer shifted 10 places to the right) %f (a real number with 6 digits after the decimal point) %.1f67.0 (a real number with 1 digit after the decimal point) %.3f ( a real number with 3 digits after the decimal point) %10.3f ( a real number with 3 digits after the decimal point, and the whole number takes 10 spaces) %cC (a character, ASCII code of 67) %e e+001 (a real number in scientific notation) Hence: fprintf(f1,'%d %5d %f %.1f %.3f %8.3f ', 67,67,67, 67, 67, 67)
26 x = 0:.2:1 fid1 = fopen('test.txt','w'); fprintf(fid1,'%6.2f \n',x); Produces on the screen x = But produces in a file named “test.txt” due to the \n : Writing vectors to file
27 >> x = 0:.2:1 x = >> y = [x ; exp(x)] y = Writing an array to a file
28 >> fprintf(fid,'%6.2f %6.3f\n',y'); Writes to the file
29 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
30 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 Table\n'); fprintf(fid,' ________________\n'); fprintf(fid,' Fahrenheit Celsius\n'); fprintf(fid,' %4i %15.2f\n',t) fclose(fid);
31 Temperature Table ________________ Fahrenheit Celsius When we open the file “Temperature.table” we find
32 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.
33 x = 0:.2:1; y = [x; exp(x)]; % Here we created it fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %6.3f\n',y); fclose(fid); fid=fopen('exp.txt','r') % 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 = Example – Let’s open and read from file we recently wrote on:
34 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 =
35 frewind 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