Nov. 28, 2005 Lecture 14 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 14 Professor Paul Lin

Slides:



Advertisements
Similar presentations
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Advertisements

Computer Science 121 Scientific Computing Winter 2012 Chapter 5 Files and Scripts.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
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.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Matlab DIY Lesson 1: Reading Data. Purpose of this Seminar Basic Ability to handle Data Analysis and Presentation in Matlab Understand how data is organized.
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
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 =
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/29/2003.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Fall 2006AE6382 Design Computing1 Matlab File & Directory Management Learning Objectives Define file input and output terminology Compare high and low.
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)
Digital Image Processing Lecture3: Introduction to MATLAB.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
1 FILE I/O: Low-level. 2 The Big Picture 3 Reading vs. Writing "Reading a file" To obtain data from a file (typically on a disk) and move a copy of it.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
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.
Cell Arrays 1.Definition 2.Creating Cell Arrays 3.Referencing Cell Arrays 4.Augmenting Cell Arrays 5.Use of Cell Arrays 1.
Introduction to File I/O High-Level Functions 1.Data files 2."High level" File I/O 3.dlmread() 4.xlsread() 1.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
CMPS 1371 Introduction to Computing for Engineers FILE Input / Output.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
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)
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
5 1 Data Files CGI/Perl Programming By Diane Zak.
GENERATION OF RANDOM NUMBERS
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
CSE123 Lecture 3 Files and File ManagementScripts.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
© 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.
File Access Storage of data in variables and arrays is temporary—such data is lost when a program terminates. Files are used for permanent retention of.
File Operations in Matlab Load / Save *.dat, *.mat vs. -ascii fopen /fclose.
Files A collection of related data treated as a unit. Two types Text
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 11”
NXT File System Just like we’re able to store multiple programs and sound files to the NXT, we can store text files that contain information we specify.
Input/Output Functions. 2 MATLAB Basics: Data Files save filename var1 var2 … save homework.mat x y  binary save x.dat x – ascii  ascii load filename.
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()
Input Output IO and datafiles Credits also to Prof Selim Aksoy (Bilkent University.
Reading and Writing Data Files
Working with files Saving and loading Matlab variables to and from .mat files does not require any special file handling, just use save() and load() However,
TMF1414 Introduction to Programming
MATLAB: Structures and File I/O
Jeff Henrikson Lecture 11 Data Import & Export Jeff Henrikson 1.
Introduction to MATLAB
Advanced Data Import & Export Jeff Henrikson
Digital Image Processing
funCTIONs and Data Import/Export
Note on Indexing of Array Elements
Introduction to MATLAB
Input/Output Functions
Input/Output Functions
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Files Chapter 8.
Presentation transcript:

Nov. 28, 2005 Lecture 14 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 14 Professor Paul Lin

Nov. 28, 2005 Lecture 14 - By Paul Lin 2 MATLAB Standard I/Os Standard I/Os Standard I/Os Standard input - keyboardStandard input - keyboard Standard output – monitor screenStandard output – monitor screen Standard error device – monitor screenStandard error device – monitor screen MATLAB I/Os MATLAB I/Os keyboard inputkeyboard input Filename = input(‘Enter a file name: ‘, ‘s’) Monitor outputMonitor output disp( ) function – display an array or text string fprintf( format,data) function – display data with desired format

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

Nov. 28, 2005 Lecture 14 - By Paul Lin 4 Save and Load Function save function save function load function 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 Current Directory

Nov. 28, 2005 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 textdlmreadDouble array. Spreadsheet formats Spreadsheet formats XLS - Excel worksheetxlsreadDouble array and cell array. WK1 - Lotus 123 worksheetwk1readDouble array and cell array. Scientific data formats Scientific data formats CDF - Common Data FormatcdfreadCell array of CDF records FITS - Flexible Image Transport System fitsreadPrimary or extension table data HDF - Hierarchical Data FormathdfreadHDF or HDF-EOS data set Movie formats Movie formats AVI - Movie avireadMATLAB movie.

Nov. 28, 2005 Lecture 14 - By Paul Lin 6 MATLAB File Formats >>help filefomats Data formats Command Returns Image formats TIFF - TIFF image imreadTruecolor, grayscale or indexed image(s). PNG - PNG imageimreadTruecolor, grayscale or indexed image. HDF - HDF imageimreadTruecolor or indexed image(s). BMP - BMP image imreadTruecolor or indexed image. JPEG - JPEG imageimreadTruecolor or grayscale image. GIF - GIF imageimreadIndexed image. PCX - PCX imageimreadIndexed image. XWD - XWD image imreadIndexed image. CUR - Cursor imageimreadIndexed image. ICO - Icon image imreadIndexed image. RAS - Sun raster image imreadTruecolor or indexed. PBM - PBM image imreadGrayscale image. PGM - PGM imageimreadGrayscale image. PPM - PPM imageimreadTruecolor image. Audio formats 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

Nov. 28, 2005 Lecture 14 - By Paul Lin 7 MATLAB Low-Level File I/Os File Opening and Closing Functions File Opening and Closing Functions fopen( ) – open file for reading, writing, or appendingfopen( ) – open file for reading, writing, or appending fclose( ) – close file or filesfclose( ) – close file or files Binary I/O Binary I/O fread( ) – read binary data from filefread( ) – read binary data from file fwirte( ) – write binary data to filefwirte( ) – write binary data to file Formatted I/O Formatted I/O fscanf( ) – read formatted data from filefscanf( ) – read formatted data from file fprintf( ) – write formatted data to filefprintf( ) – write formatted data to file fgets( ) – read line from file, keep newline characterfgets( ) – read line from file, keep newline character fgetl( ) – read line form file, discard newline characterfgetl( ) – read line form file, discard newline character

Nov. 28, 2005 Lecture 14 - By Paul Lin 8 MATLAB Low-Level File I/Os (continued) String Conversion Functions String Conversion Functions sprinf( ) – write formatted data to stringsprinf( ) – write formatted data to string sscanf( ) – read string under format controlsscanf( ) – read string under format control File Positioning Functions File Positioning Functions ferros( ) – File I/O error statusferros( ) – File I/O error status feof( ) – test for end of filefeof( ) – test for end of file fseek( ) – set file position indicatorfseek( ) – set file position indicator ftell( ) – get file position indicatorftell( ) – get file position indicator frewind( ) – Rewind filefrewind( ) – Rewind file

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

Nov. 28, 2005 Lecture 14 - By Paul Lin 10 Example 14-1: Loading a Sequence of Data Files Create dataFiles 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 To open a sequence of enumerated data files for file_no = 1: N fname = sprintf(‘testData%d’,file_no ); load(fname)end Create dataFiles Create dataFiles >> whos Name Size Bytes Class N 1x1 8 double array N 1x1 8 double array file_no 1x1 8 double array Fname 1x9 18 char array x 1x double array y 1x double array z 1x double array >> plot(x, y) >> plot(x, z)

Nov. 28, 2005 Lecture 14 - By Paul Lin 11 File Processing Reading a file Open a file for reading Open a file for reading Process reading from the file Process reading from the file Close the file Close the file Appending to a file Open a file for appending Open a file for appending Process appending the file Process appending the file Close the file Close the file Writing to a file Open a file for writing Open a file for writing Process writing to the file Process writing to the file Close the file Close the file

Nov. 28, 2005 Lecture 14 - By Paul Lin 12 Typical File Processing Open files Open files fid = fopen(' file_name ', permissions), where permissions can be one of the following: 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. 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'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‘ - Open the file for writing, create if necessary 'w+‘ - Truncate or create for read and write'w+‘ - Truncate or create for read and write 'a‘ - Append (create if necessary)'a‘ - Append (create if necessary) 'a+ ‘ – Read and append (create if necessary)'a+ ‘ – Read and append (create if necessary) Write to, Read from files Write to, Read from files Close files Close files

Nov. 28, 2005 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:

Nov. 28, 2005 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

Nov. 28, 2005 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

Nov. 28, 2005 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

Nov. 28, 2005 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'); count = fwrite(fid, out_data, 'double'); disp([int2str(count) ' value written to disk..']); disp([int2str(count) ' value written to disk..']); status = fclose(fid); status = fclose(fid);else % Failed to open the file % Failed to open the file disp(msg); disp(msg);end % Open the file for reading [fid msg] = fopen(filename, 'r'); if fid > 0 [in_data, count] = fread(fid, 10, 'double'); [in_data, count] = fread(fid, 10, 'double'); disp([int2str(count) ' value read from the disk..']); disp([int2str(count) ' value read from the disk..']); status = fclose(fid); status = fclose(fid);else % Failed to open the file % Failed to open the file disp(msg); disp(msg);endin_dataout_data

Nov. 28, 2005 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'); count = fwrite(fid, out_data, 'double'); disp([int2str(count) ' value written to disk..']); disp([int2str(count) ' value written to disk..']); status = fclose(fid); status = fclose(fid);else % Failed to open the file % Failed to open the file disp(msg); disp(msg);end % Open the file for reading [fid msg] = fopen(filename, 'r'); if fid > 0 [in_data, count] = fread(fid, 10, 'double'); [in_data, count] = fread(fid, 10, 'double'); disp([int2str(count) ' value read from the disk..']); disp([int2str(count) ' value read from the disk..']); status = fclose(fid); status = fclose(fid);else % Failed to open the file % Failed to open the file disp(msg); disp(msg);endin_dataout_data

Nov. 28, 2005 Lecture 14 - By Paul Lin 19 Example 14-4: Writing and Reading Binary Data in_data = out_data = Binary Files - Not Readable

Nov. 28, 2005 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