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

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Lesson 2: Basic Output You’ve got data……now what???
Lesson 2: Basic Output You’ve got data……now what???
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Lecture 5 Input and Output inputfprintf © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
FILE I/O: Low-level General Ideas. High vs. low-level Opening a file
File IO and command line input CSE 2451 Rong Shi.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
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)
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Asking the USER for values to use in a software 1 Input.
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
EGR 115 Introduction to Computing for Engineers MATLAB Basics 3: Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Today. by 5, by Discuss on Friday. –Homework IV: on web, due following.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 3 Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Reading and Writing Data Files
EGR 115 Introduction to Computing for Engineers
Lecture: MATLAB Chapter 1 Introduction
File Access (7.5) CSE 2031 Fall July 2018.
File Input/Output.
MATLAB: Structures and File I/O
Computer Programming Lecture 15 Text File I/O
Programming in C Input / Output.
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Introduction to MATLAB
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Matlab Training Session 5: Importing Data
Note on Indexing of Array Elements
Input/Output Functions
Programming in C Input / Output.
Input/Output Functions
Files Chapter 8.
Presentation transcript:

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

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

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

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

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

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

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

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

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 = [ ]; 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

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 = [ ; ; ]; Try one of the three methods to create the file!! Slide 11 of 18

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', ) >> 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

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

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 = count = 6 Slide 14 of 18

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 = count = 1 my_var = count = 1 my_var = count = 1 Close the file !! - fclose(fid); Slide 15 of 18

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 = my_var = fscanf(fid, '%f', 2) % Note that the size is TRANSPOSED!! my_var = Close the file !! - fclose(fid); Slide 16 of 18

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 = Slide 17 of 18

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