We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Computer Programming
with MATLAB
Advanced File Input and Output
Opening A File Closing A File Reading from Files Writing to Files Exercise
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Advanced File Input and Output input function is used to read values entered by the user. disp and fprintf are used to display information in windows on the screen. For file input and output (file I/O), we use the load and save functions, which can read from a data file into a matrix, and write from a matrix to a data file. There are many different file types, which use different filename extensions. load and save function works only with either the extension .dat or .txt. The load and save command works only if there are the same number of values in each line and the values are the same type, so that the data can be stored in a matrix. BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 Steps involved in File Operation Open the file. Read from the file, write to the file, or append to the file. Close the file.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Opening A File fopen This is used to open a particular file. By default, the fopen function opens a file for reading. If another mode is desired, a permission string is used to specify which mode (e.g., writing or appending). The general form is
◼ The permission string may
be:
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Opening A File ⚫ Always remember, when fopen is attempted, the value returned should be tested to make sure that the file was successfully opened or not.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Closing A ⚫ File fclose ◼ This is used to close the opened file. Individual files can be closed by specifying the file identifier or if more than one file is open, all open files can be closed by passing the string ‘all’ to the close function.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Writing to Files fprintf This is used to write one line at a time to a file. This is used to write a file and also to append something to a file. Also used to write output on the screen. The screen is the default output device, so if a file identifier is not specified, the output goes to the screen otherwise it goes to the specified file.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Reading from A File fgetl This is used to read strings from a file one line at a time. This function reads one line of data from a file into a string; string functions can then be used to manipulate the data. Since fgetl reads only one line, it normally is placed in a loop that keeps going until the end of the file is reached. (feof function is used for the same)
Question Consider data file ‘subjexp.dat’ which contains a number followed by a character code on each line. WAP to display contents as shown below
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Reading from A File
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise Question For a biomedical experiment, the names and weights of some patients have been stored in a file ‘patwts.dat’. For example, the file might look like this: ◼ Darby George 166.2 ◼ Helen Dee 143.5 ◼ Giovanni Lupa 192.4 ◼ Cat Donovan 215.1
⚫ Create this data file
first.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise ◼ Question (From patwts.dat read and display data as follows:) ⚫ George,Darby 166.2 ⚫ Dee,Helen 143.5 ⚫ Lupa,Giovanni 192.4 ⚫ Donovan,Cat 215.1 The average weight is 179.30 File close successful.
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Ombudsman Complaint Filed at Victorian Ombudsman Redacted Dvs Aug 1 2015 before Associate Professor Dr Peter Doherty Psychiatrist and Michael McGarvie wanted to know what the anti-pedophile nurses group told US law agencies n 2014
PDF (Ebook) Thoracic Imaging: Case Review Series by Theresa C. McLoud MD, Phillip M. Boiselle MD, Gerald F. Abbott MD ISBN 9780323029995, 032302999X download