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

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

Embed Size (px)

Citation preview

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

EGR 115 Introduction to Computing for Engineers

Formatted File Input / Output

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

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

Lecture Outline

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Formatted File Input / Output fprintf and fscanf

Slide 2 of 18

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

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-formattedo Print formatted data to the Command Window or to a file

fscanfo Can be used to read formatted data from a file

Slide 3 of 18

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

Formatted File Input / OutputTop-Level File I/O Summary

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Typical procedure for a file read/write operation1. Open the file (fopen(…))

o Creates a file identifier for use by future operations2. 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

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

Formatted File Input / Outputfopen

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Open the file (fopen(…)) fid = fopen(‘filename’, ‘permission_string’);

o fid: The file identifierWill 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.txto permission_string:

Read: ‘r’Write: ‘w’Append: ‘a’

Slide 5 of 18

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

Formatted File Input / Outputfopen

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 writingo Why did it work?? - > File was created!!

Look and see what is in the file

% Open a file in the current dir for readingfid = fopen('my_file.txt','r'); if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

Slide 6 of 18

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

Formatted File Input / Outputfclose

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 file3. 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

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

Formatted File Input / Outputfprintf

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 identifiero 'format' - the format specifiero var1, var2, ...: Variables to be printed

Slide 8 of 18

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

Formatted File Input / Outputfprintf

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 WRITINGfid = fopen('my_file.txt','w'); if fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

fprintf('my value of A = %5.3f and B = %d - %s\n', a, b, c); % Print to screen

Newline character

Slide 9 of 18

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

Formatted File Input / Outputfprintf

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 precisiono One row of the Matrix per line in the file with space between

each number:

Slide 10 of 18

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

Formatted File Input / Outputfprintf – 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 messageend

%% Method #1: Brute Forcefprintf(fid, '%5.2f %5.2f\n', M(1,1), M(1,2)); % Print to filefprintf(fid, '%5.2f %5.2f\n', M(2,1), M(2,2)); % Print to filefprintf(fid, '%5.2f %5.2f\n', M(3,1), M(3,2)); % Print to file

%% Method #2: Print one row at a timefor row = 1:3 fprintf(fid, '%5.2f %5.2f\n', M(row,1), M(row,2)); % Print to fileend

%% Method #3: Print one element at a time[Nrows, Ncols] = size(M); % Determine the number of rows and columnsfor 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 lineendfclose(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

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

Formatted File Input / OutputA 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.57o 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

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

Formatted File Input / Outputfscanf

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 identifiero 'format' - the format specifiero var: Store the data read into the variable ‘var’o count: The number of values reado size: The size of the data to be read

Slide 13 of 18

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

Formatted File Input / Outputfscanf – 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!!

%% fscanffid = fopen('my_matrix.txt','r'); % Open for readingif fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

% 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.7100count = 6

Slide 14 of 18

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

Formatted File Input / Outputfscanf – 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)

%% fscanffid = fopen('my_matrix.txt','r'); % Open for readingif fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

[my_var, count] = fscanf(fid, '%f', 1)

[my_var, count] = fscanf(fid, '%f', 1)

my_var = 6.9700count = 1

my_var = 8.3400count = 1

my_var = 3.0700count = 1Close the file !! - fclose(fid);

Slide 15 of 18

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

Formatted File Input / Outputfscanf – Formatted Reading

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Method#3: Lets try to read one row at a time%% fscanffid = fopen('my_matrix.txt','r'); % Open for readingif fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

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.1600Close the file !! - fclose(fid);

Slide 16 of 18

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

Formatted File Input / Outputfscanf – Formatted Reading

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Method#4: Lets try to read the matrix all at once

%% fscanffid = fopen('my_matrix.txt','r'); % Open for readingif fid == -1 % fopen failed!! Can NOT open the file error('Cannot open the file!!'); % Print an error messageend

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

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

Next Lecture

Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers

• Cell Arrays & Structures

Slide 18 of 18