1 / 20

CPET 190

CPET 190. Problem Solving with MATLAB Lecture 14 Professor Paul Lin http://www.etcs.ipfw.edu/~lin. MATLAB Standard I/Os. Standard I/Os Standard input - keyboard Standard output – monitor screen Standard error device – monitor screen MATLAB I/Os keyboard input

nile
Télécharger la présentation

CPET 190

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPET 190 Problem Solving with MATLAB Lecture 14 Professor Paul Lin http://www.etcs.ipfw.edu/~lin Lecture 14 - By Paul Lin

  2. MATLAB 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 Lecture 14 - By Paul Lin

  3. Lecture 14: File Input and Output • MATLAB I/O Functions • diary( ) – save text of MATLAB session >> diary • save( ) – save workspace variable to disk >> save FILENAME • load( ) – load workspace variables from disk >> load FILENAME • textread( ) – read formatted data from text file Lecture 14 - By Paul Lin

  4. Save and Load Function >> save 8_20_2004.mat >> clear >> whos >> load 8_20_2004.mat >> 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 >> clear >> whos • save function • load function Current Directory Lecture 14 - By Paul Lin

  5. MATLAB File Formats >>help 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. Lecture 14 - By Paul Lin

  6. MATLAB File Formats >>help 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 AU - NeXT/Sun sound auread Sound data and sample rate. SND - NeXT/Sun sound auread Sound data and sample rate. WAV - Microsoft Wave sound wavread Sound data and sample rate Lecture 14 - By Paul Lin

  7. MATLAB 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 Lecture 14 - By Paul Lin

  8. MATLAB 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 Lecture 14 - By Paul Lin

  9. File Path Set File Path • File -> Set Path -> Add Folder • MTALAB Path Search -> G:\public_html\cpet190\codes • Click on Save button, and then Close button Lecture 14 - By Paul Lin

  10. Example 14-1: Loading a Sequence of Data Files • Create dataFiles >> 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 >> plot(x , y) >> plot(x, z) • Create dataFiles >> x = -10:0.1:10; >> y = 2*(x.^2) + 1; >> save dataFile1 >> clear y >> y = 2*(x.^2) + 10; >> save dataFile2 • To open a sequence of enumerated data files for file_no = 1: N fname = sprintf(‘testData%d’,file_no); load(fname) end Lecture 14 - By Paul Lin

  11. File 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 Lecture 14 - By Paul Lin

  12. Typical 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 Lecture 14 - By Paul Lin

  13. Example 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:.1:1; 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 Lecture 14 - By Paul Lin

  14. Example 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 Lecture 14 - By Paul Lin

  15. Example 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 Lecture 14 - By Paul Lin

  16. Example 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 Lecture 14 - By Paul Lin

  17. Example 14-4: Writing and Reading Binary Data %binary_file.m filename = input('Enter a file name: ', 's'); out_data = 100*rand(10, 1); [fid msg] = fopen(filename,'w'); % File open OK? if fid > 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 > 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 Lecture 14 - By Paul Lin

  18. Example 14-4: Writing and Reading Binary Data %binary_file.m filename = input('Enter a file name: ', 's'); out_data = 100*rand(10, 1); [fid msg] = fopen(filename,'w'); % File open OK? if fid > 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 > 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 Lecture 14 - By Paul Lin

  19. Example 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 Lecture 14 - By Paul Lin

  20. Summary 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 Lecture 14 - By Paul Lin

More Related