Title: CPET 190
1CPET 190
- Problem Solving with MATLAB
- Lecture 14
- Professor Paul Lin
- http//www.etcs.ipfw.edu/lin
2MATLAB Standard I/Os
- Standard I/Os
- Standard input - keyboard
- Standard output monitor screen
- Standard error device monitor screen
- MATLAB I/Os
- keyboard input
- Filename input(Enter a file name , s)
- Monitor output
- disp( ) function display an array or text
string - fprintf( format,data) function display data
with desired format
3Lecture 14 File Input and Output
- MATLAB I/O Functions
- diary( ) save text of MATLAB session
- gtgt diary
- save( ) save workspace variable to disk
- gtgt save FILENAME
- load( ) load workspace variables from disk
- gtgt load FILENAME
- textread( ) read formatted data from text file
4Save and Load Function
- save function
- load function
gtgt save 8_20_2004.mat gtgt clear gtgt whos gtgt load
8_20_2004.mat gtgt whos Name Size Bytes
Class ans 1x1 8 double array
x 1x1 8 double array y
1x1 8 double array Grand total
is 3 elements using 24 bytes gtgt clear gtgt whos
Current Directory
5MATLAB File Formats
- gtgthelp filefomats
- Data formats Commands Returns
- MAT - MATLAB workspace load Variables in
file. - CSV - Comma separated numbers csvread
Double array. - DAT - Formatted text importdata Double array.
- DLM - Delimited text dlmread Double array.
- TAB - Tab separated text dlmread Double array.
-
- Spreadsheet formats
- XLS - Excel worksheet xlsread Double array and
cell array. - WK1 - Lotus 123 worksheet wk1read Double array
and cell array. -
- Scientific data formats
- CDF - Common Data Format cdfread Cell array of
CDF records - FITS - Flexible Image Transport System
fitsread Primary or extension table data - HDF - Hierarchical Data Format hdfread HDF or
HDF-EOS data set - Movie formats
- AVI - Movie aviread MATLAB movie.
6MATLAB File Formats
- gtgthelp filefomats
- Data formats Command Returns
- Image formats
- TIFF - TIFF image imread Truecolor, grayscale or
indexed image(s). - PNG - PNG image imread Truecolor, grayscale or
indexed image. - HDF - HDF image imread Truecolor or indexed
image(s). - BMP - BMP image imread Truecolor or indexed
image. - JPEG - JPEG image imread Truecolor or grayscale
image. - GIF - GIF image imread Indexed image.
- PCX - PCX image imread Indexed image.
- XWD - XWD image imread Indexed image.
- CUR - Cursor image imread Indexed image.
- ICO - Icon image imread Indexed image.
- RAS - Sun raster image imread Truecolor or
indexed. - PBM - PBM image imread Grayscale image.
- PGM - PGM image imread Grayscale image.
- PPM - PPM image imread Truecolor image.
-
- Audio formats
7MATLAB Low-Level File I/Os
- File Opening and Closing Functions
- fopen( ) open file for reading, writing, or
appending - fclose( ) close file or files
- Binary I/O
- fread( ) read binary data from file
- fwirte( ) write binary data to file
- Formatted I/O
- fscanf( ) read formatted data from file
- fprintf( ) write formatted data to file
- fgets( ) read line from file, keep newline
character - fgetl( ) read line form file, discard newline
character
8MATLAB Low-Level File I/Os (continued)
- String Conversion Functions
- sprinf( ) write formatted data to string
- sscanf( ) read string under format control
- File Positioning Functions
- ferros( ) File I/O error status
- feof( ) test for end of file
- fseek( ) set file position indicator
- ftell( ) get file position indicator
- frewind( ) Rewind file
9File Path
- Set File Path
- File -gt Set Path -gt Add Folder
- MTALAB Path Search -gt G\public_html\cpet190\codes
- Click on Save button, and then Close button
10Example 14-1 Loading a Sequence of Data Files
- Create dataFiles
- gtgt x -100.110
- gtgt y 2(x.2) 1
- gtgt save dataFile1
- gtgt clear y
- gtgt y 2(x.2) 10
- gtgt save dataFile2
- To open a sequence of enumerated data files
- for file_no 1 N
- fname sprintf(testDatad,file_no)
- load(fname)
- end
- Create dataFiles
- gtgt whos
- Name Size Bytes Class
- N 1x1 8 double array
- file_no 1x1 8 double array
- Fname 1x9 18 char array
- x 1x201 1608 double array
- y 1x201 1608 double array
- z 1x201 1608 double array
- gtgt plot(x , y)
- gtgt plot(x, z)
11File Processing
- Reading a file
- Open a file for reading
- Process reading from the file
- Close the file
- Appending to a file
- Open a file for appending
- Process appending the file
- Close the file
- Writing to a file
- Open a file for writing
- Process writing to the file
- Close the file
12Typical File Processing
- Open files
- fid fopen(' file_name ', permissions), where
permissions can be one of the following - 'r - Open the file for reading. The file must
exist. An error occurs if the file does not
exist. - 'r - Open the file for reading and writing. The
file mut exist. An error occure if the file does
not exist - 'w - Open the file for writing, create if
necessary - 'w - Truncate or create for read and write
- 'a - Append (create if necessary)
- 'a Read and append (create if necessary)
- Write to, Read from files
- Close files
13Example 14-2 Create a file - a table of the
exponential function
- Pseudo Code
- 1. Create x and y vectors for exponential
function - 2. Open A File for Writing
- 3. Write Formatted Data fprinf(fid, format
specifiers, data) - 4. Close File
- The program
- save_exp.m
- x 0.11 y x' exp(x)'
- fid fopen('expfucntion.txt','w')
- fprintf(fid,'f f\n', y)
- fclose(fid)
File Content 0 1.0000 0.1000
1.1052 0.2000 1.2214 0.3000 1.3499
0.4000 1.4918 0.5000 1.6487
0.6000 1.8221 0.7000 2.0138 0.8000
2.2255 0.9000 2.4596 1.0000 2.7183
14Example 14-2 Create a file - a table of the
exponential function
- We edit the program as shown in previous slide
- Set default directory or folder for storing the
file - Debug or test the program through the MATLAB
M-file editor - Open Windows Explorer and examine the created
file
15Example 14-2 Create a file - a table of the
exponential function
- Open the expfunction.txt file using Microsoft
Wordpad - Open the expfunction.txt file using Notepad
16Example 14-3 Reading Exponential Function File
for Plotting
- load_exp.m
- fid fopen('expfucntion.txt','r')
- Retrieve data table a 11-by-2
- y1 count fscanf(fid,'f', 11 2)
- fclose(fid)
- Extract first column
- x y1(,1)
- Extract 2nd column
- exp_x y1(,2)
- plot(x, exp_x) grid on
17Example 14-4 Writing and Reading Binary Data
- binary_file.m
- filename input('Enter a file name ', 's')
- out_data 100rand(10, 1)
- fid msg fopen(filename,'w')
- File open OK?
- if fid gt 0
- count fwrite(fid, out_data, 'double')
- disp(int2str(count) ' value written to disk
..') - status fclose(fid)
- else
- Failed to open the file
- disp(msg)
- end
Open the file for reading fid msg
fopen(filename, 'r') if fid gt 0 in_data,
count fread(fid, 10, 'double')
disp(int2str(count) ' value read from the disk
..') status fclose(fid) else
Failed to open the file disp(msg) end in_data
out_data
18Example 14-4 Writing and Reading Binary Data
- binary_file.m
- filename input('Enter a file name ', 's')
- out_data 100rand(10, 1)
- fid msg fopen(filename,'w')
- File open OK?
- if fid gt 0
- count fwrite(fid, out_data, 'double')
- disp(int2str(count) ' value written to disk
..') - status fclose(fid)
- else
- Failed to open the file
- disp(msg)
- end
Open the file for reading fid msg
fopen(filename, 'r') if fid gt 0 in_data,
count fread(fid, 10, 'double')
disp(int2str(count) ' value read from the disk
..') status fclose(fid) else
Failed to open the file disp(msg) end in_data
out_data
19Example 14-4 Writing and Reading Binary Data
- in_data
- 95.0129
- 23.1139
- 60.6843
- 48.5982
- 89.1299
- 76.2097
- 45.6468
- 1.8504
- 82.1407
- 44.4703
out_data 95.0129 23.1139 60.6843
48.5982 89.1299 76.2097 45.6468
1.8504 82.1407 44.4703
Binary Files - Not Readable
20Summary
- MATLAB Standard I/Os - File Input and Output
- Save and Load Function
- MATLAB File Formats
- MATLAB Low-Level File I/Os
- File Path
- File Processing Examples
- Formatted Files
- Binary Files
- Be sure to read Chapter 8 of the Text book,
MATLAB help