Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Formatted File Input / Output  fprintf and fscanf Slide 2 of 18

3 Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers MATLAB Supports “C-Like” I/O Functions  fprintf o Stands for file-print-formatted o Print formatted data to the Command Window or to a file  fscanf o Can be used to read formatted data from a file Slide 3 of 18

4 Formatted File Input / Output Top-Level File I/O Summary Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Typical procedure for a file read/write operation 1.Open the file (fopen(…)) o Creates a file identifier for use by future operations 2.Perform read(s)/write(s) from/to a file o Need to specify the data format for read/write operation(s) 3.Close the file (fclose(…)) o Releases the file to be useable by other programs  The OS does not like having too many files open at once Printing to the Command Window does NOT require the file open or close operations  Does, however, require data formatting Slide 4 of 18

5 Formatted File Input / Output fopen Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Open the file (fopen(…))  fid = fopen(‘filename’, ‘permission_string’); o fid: The file identifier  Will be required by the read/write command to identify what file to read/write from/to. o filename: any valid filename.  E.g., my_file1.txt o permission_string:  Read: ‘ r ’  Write: ‘ w ’  Append: ‘ a ’ Slide 5 of 18

6 Formatted File Input / Output fopen Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Open the file (fopen(…)):  You should always confirm that the “fopen” was successful  Try this!! o Why did it fail?? - > File did not exist!!  Change your code to open the file for writing o Why did it work?? - > File was created!!  Look and see what is in the file % Open a file in the current dir for reading fid = fopen('my_file.txt','r'); if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end Slide 6 of 18

7 Formatted File Input / Output fclose Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Close the file (fopen(file_id)): 1.Open the file (fopen(…)) 2.Perform some writing/reading to/from the file 3.When finished close the file fid = fopen('my_file.txt','w'); % Open the file... % - Print some “stuff” in here... fclose(fid); % Close the file Slide 7 of 18

8 Formatted File Input / Output fprintf Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Perform write operation to a file  Remember to open the file for writing beforehand  fprintf(fid, 'format', var1, var2,...); o fid - the previously defined file identifier o 'format' - the format specifier o var1, var2,... : Variables to be printed Slide 8 of 18

9 Formatted File Input / Output fprintf Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers An Example of writing (fprintf): fprintf(fid, '%5.3f %d %s\n', a, b, c); % Print to file a = pi; b = 3; c = 'BYE!!'; % Open a file in the current dir for WRITING fid = fopen('my_file.txt','w'); if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end fprintf('my value of A = %5.3f and B = %d - %s\n', a, b, c); % Print to screen Newline character Slide 9 of 18

10 Formatted File Input / Output fprintf Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers An In-Class Assignment:  Write a matrix “M” to the file “my_matrix.txt” M = [6.9722 8.3388 3.0686 4.1616 6.1420 1.7059]; o Write the values with two digits of precision o One row of the Matrix per line in the file with space between each number: Slide 10 of 18

11 Formatted File Input / Output fprintf – Solution to In-Class Assignment Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers % Write a matrix “M” to the file “my_matrix.txt” fid = fopen('my_matrix.txt','w'); if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end % Method #1: Brute Force fprintf(fid, '%5.2f %5.2f\n', M(1,1), M(1,2)); % Print to file fprintf(fid, '%5.2f %5.2f\n', M(2,1), M(2,2)); % Print to file fprintf(fid, '%5.2f %5.2f\n', M(3,1), M(3,2)); % Print to file % Method #2: Print one row at a time for row = 1:3 fprintf(fid, '%5.2f %5.2f\n', M(row,1), M(row,2)); % Print to file end % Method #3: Print one element at a time [Nrows, Ncols] = size(M); % Determine the number of rows and columns for row = 1:Nrows for col = 1:Ncols fprintf(fid, '%5.2f ', M(row,col)); % Print to file end fprintf(fid, '\n'); % Go to the next line end fclose(fid); M = [6.9722 8.3388; 3.0686 4.1616; 6.1420 1.7059]; Try one of the three methods to create the file!! Slide 11 of 18

12 Formatted File Input / Output A Short Quiz Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Quiz – Determine the output from:  fprintf('%3.2f\n', 1234.5678) >> 1234.57 o Overrides the field width (of 3), but, preserves the precision (of 2)  fprintf(‘Int is %2.0f and float is %6.2f\n', pi, pi) >> Int is 3 and float is 3.14  fprintf('The value is %d, surely!',4^3) The value is 64, surely!>> o Note that the newline char (i.e., \n) is missing!!  fprintf('The value is %d, \nOK!\n', 4^3) >>The value is 64, surely!The value is 64, >>OK! Slide 12 of 18

13 Formatted File Input / Output fscanf Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Perform read operation from a file  Remember to open the file for reading beforehand  [var, count] = fscanf(fid, 'format‘, size); o fid - the previously defined file identifier o 'format' - the format specifier o var: S tore the data read into the variable ‘var’ o count : The number of values read o size : The size of the data to be read Slide 13 of 18

14 Formatted File Input / Output fscanf – Formatted Reading Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Read data from the file “my_matrix.txt”  Try it !!!  The “fscanf” reads row by row!! % fscanf fid = fopen('my_matrix.txt','r'); % Open for reading if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end % Method #1: Read all the values into an array [my_var, count] = fscanf(fid, '%f') fclose(fid); Notice the order in which the variables are read!! my_var = 6.9700 8.3400 3.0700 4.1600 6.1400 1.7100 count = 6 Slide 14 of 18

15 Formatted File Input / Output fscanf – Formatted Reading Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Method#2: Lets try to read one value at a time  This illustrates the order of reading [my_var, count] = fscanf(fid, '%f', 1) % fscanf fid = fopen('my_matrix.txt','r'); % Open for reading if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end [my_var, count] = fscanf(fid, '%f', 1) my_var = 6.9700 count = 1 my_var = 8.3400 count = 1 my_var = 3.0700 count = 1 Close the file !! - fclose(fid); Slide 15 of 18

16 Formatted File Input / Output fscanf – Formatted Reading Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Method#3: Lets try to read one row at a time % fscanf fid = fopen('my_matrix.txt','r'); % Open for reading if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end my_var = fscanf(fid, '%f', 2) % Note that the size is TRANSPOSED!! my_var = 6.9700 8.3400 my_var = fscanf(fid, '%f', 2) % Note that the size is TRANSPOSED!! my_var = 3.0700 4.1600 Close the file !! - fclose(fid); Slide 16 of 18

17 Formatted File Input / Output fscanf – Formatted Reading Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Method#4: Lets try to read the matrix all at once % fscanf fid = fopen('my_matrix.txt','r'); % Open for reading if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error message end my_var = fscanf(fid, '%f', [2,3]); % Note the size is TRANSPOSED!! my_var = my_var' my_var = 6.9700 8.3400 3.0700 4.1600 6.1400 1.7100 Slide 17 of 18

18 Next Lecture Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers Cell Arrays & Structures Slide 18 of 18


Download ppt "EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google