350 likes | 413 Views
Input – reads/gets data for the program Output – the product, after processing. Input / Output. Both can be: interactive I/O (while program is running) (RAM memory) or file I/O : (read/write to file) (Hard disk Memory). Built-in functions for interactive input input menu
E N D
Input – reads/gets data for the program Output – the product, after processing Input / Output Both can be: interactive I/O (while program is running) (RAM memory) or file I/O: (read/write to file) (Hard disk Memory)
Built-in functions for interactive input • input • menu • keyboard • (pause) Interactive Input
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?
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
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
Example >> 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
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
PopOption=menu(‘Initial size','200', '250','300','350') PopOption = 4 PopInitials=[200, 250, 300, 350]; Pop=PopInitials(PopOption) Pop= 350
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
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
plot(x,y)Plots y as a function of x Often very useful to display results Example from your program Basics: Pop = 1.0000 240.0000 48.0000 30.0000 18.0000 2.0000 258.0000 51.6000 32.2500 19.3500 3.0000 277.3500 55.4700 34.6688 20.8013 4.0000 298.1513 59.6303 37.2689 22.3613 5.0000 320.5126 64.1025 40.0641 24.0384 6.0000 344.5510 68.9102 43.0689 25.8413 7.0000 370.3924 74.0785 46.2990 27.7794 8.0000 398.1718 79.6344 49.7715 29.8629 9.0000 428.0347 0 0 0 10.0000 0 0 0 0
Reading-from and writing-to a file Save Load fopen fclose fprintf fscanf frewind File Input/Output Writing and reading MATLAB data General text files
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. • `
save('fileName',varName) • Saves the variable “varName” in a file named filename.mat
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.
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)
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 = -1 27
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
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-
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 1.500000
fprintf(fid,'The answer is %d not %f\n',42,1.5); Specifier (integer) Conversion specification (floating point) Special character (new line)
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-
Specifieroutput meaning %d67 (an integer) %10d67 (an integer shifted 10 places to the right) %f67.000000 (a real number with 6 digits after the decimal point) %.1f67.0 (a real number with 1 digit after the decimal point) %.3f67.000 ( a real number with 3 digits after the decimal point) %10.3f67.000 ( a real number with 3 digits after the decimal point, and the whole number takes 10 spaces) %cC (a character , ASCII code of 67) %e6.700000e+001 (a real number in scientific notation) Hence: fprintf(f1,'%d %5d %f %.1f %.3f %8.3f ', 67,67,67, 67, 67, 67) 67 67 67.000000 67.0 67.000 67.000
Writing vectors to file x = 0:.2:1 fid1 = fopen('test.txt','w'); fprintf(fid1,'%6.2f \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
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 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.2214 1.4918 1.8221 2.2255 2.7183
>> fprintf(fid,'%6.2f %6.3f\n',y'); 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
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
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);
Temperature Table ________________ Fahrenheit Celsius -40 -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. When we open the file “Temperature.table” we find
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.
Example – Let’s open and read from file we recently wrote on: 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 = 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.2210 1.4920 1.8220 2.2260 2.7180
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 0.2000 0.4000 1.0000 1.2210 1.4920
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