Chapter8-11 - Chapter8 FileInputandOutput WhyFiles? Thus...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
             Chapter 8 File Input and Output
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Thus far we have only looked at reading data from the keyboard and writing data to the screen. We will now look at reading data from files and writing data to files. When we read data from a file, the file could have been created by MATLAB or another program. This will allow us to exchange data with other programs.          Why Files?
Background image of page 2
For example, we could create a data file with a data acquisition program. MATLAB could then read the file and perform numerical calculations on the data and/or graph the data By writing data to files, MATLAB can export data that can be read by other programs. Another advantage of reading and writing files is that files stored on disk are non-volatile. That is, when we shut off the computer the data will be saved in the file. The next time we turn on the computer and run MATLAB, we can read the file and begin where we left off.            Why Files?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File I/O Command structure Files are identified by file id numbers that are assigned by MATLAB. These are called fid numbers. First a file must be assigned a fid. In addition it must be opened with permission to do something: 'w' write 'r' read 'a' append
Background image of page 4
We must first open the files for writing. Since we are creating new files, we must use either the 'a' or 'w' permission. fid1=fopen('file1.txt', 'w'); fid2=fopen('file2.txt', 'w'); fid3=fopen('file3.txt', 'w'); First we will not specify a file identifier in the fprintf function. The output will be written to the MATLAB command window: fprintf('This is a test. No FID specified.\n'); When we do not specify a file identifier ( fid returned by using the fopen function), the fprintf function automatically sends the output to the screen.          Creating Files Copy and paste  Class_Example_8_1 in to  own m-file
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
We will now write to file1.txt: fprintf( fid1, ' This is text sent to the file indicated by fid1.\n'); fprintf( fid1, ' We will generate a few lines.\n'); fprintf( fid1, ' The value of fid1 is %g\n', fid1); Notice that output is not displayed on the command window.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/13/2010 for the course EGR EGR115 taught by Professor Newman during the Fall '10 term at Embry-Riddle FL/AZ.

Page1 / 23

Chapter8-11 - Chapter8 FileInputandOutput WhyFiles? Thus...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online