M AT L AB Reading & Writing Files. Low-level file input and output is very similar to that in 'C', but with inherent vectorization. Files are opened with.

Slides:



Advertisements
Similar presentations
Introduction to M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Simple Data Type Representation and conversion of numbers
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
 Pearson Education, Inc. All rights reserved Formatted Output.
C Tokens Identifiers Keywords Constants Operators Special symbols.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
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 Input and Output
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Today. by 5, by Discuss on Friday. –Homework IV: on web, due following.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Characters and Strings
©Brooks/Cole, 2003 Chapter 3 Number Representation.
CS 1704 Introduction to Data Structures and Software Engineering.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
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,
CSE 220 – C Programming Bitwise Operators.
Number Representation
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Input/output.
TMF1414 Introduction to Programming
File Access (7.5) CSE 2031 Fall July 2018.
EPSII 59:006 Spring 2004.
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
CS1010 Programming Methodology
What to bring: iCard, pens/pencils (They provide the scratch paper)
Programming in C Input / Output.
Input and Output Lecture 4.
Chapter 08- printf and scanf
Programming in C Input / Output.
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File Input and Output.
Differences between Java and C
Programming in C Input / Output.
Conversion Check your class notes and given examples at class.
Module 12 Input and Output
Topics Introduction to File Input and Output
Professor Jodi Neely-Ritz University of Florida
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

M AT L AB Reading & Writing Files

Low-level file input and output is very similar to that in 'C', but with inherent vectorization. Files are opened with >> fid = fopen(filename) >> fid = fopen(filename,mode) fid is a file identifier, used as a reference to the file for all subsequent read and write operations Files are closed with: >> fclose(fid)

fopen modes 'r'Open file for reading (default). 'w'Open file, or create new file, for writing; discard existing contents, if any. 'a'Open file, or create new file, for writing; append data to the end of the file. 'r+'Open file for reading and writing. 'w+'Open file, or create new file, for reading and writing; discard existing contents, if any. 'a+'Open file, or create new file, for reading and writing; append data to the end of the file. 'A'Append without automatic flushing; used with tape drives. 'W'Write without automatic flushing; used with tape drives.

'cray' or 'c' Cray floating point with big-endian byte ordering 'ieee-be' or 'b' IEEE floating point with big-endian byte ordering 'ieee-le' or 'l' IEEE floating point with little-endian byte ordering 'ieee-be.l64' or 's'IEEE floating point with big-endian byte ordering and 64-bit long data type 'ieee-le.l64' or 'a'IEEE floating point with little-endian byte ordering and 64-bit long data type 'native' or 'n' Numeric format of the machine on which MATLAB is running (the default) 'vaxd' or 'd' VAX D floating point and VAX ordering 'vaxg' or 'g' VAX G floating point and VAX ordering >> fopen(filename,mode,format) Opens a binary file and treats all data read or written as being of the specified format. This may be necessary to read binary files written under a different operating system. FORMATS

Reading Binary Data >> data = fread(fid) >> data = fread(fid,count,precision,skip,format) NB. All input parameters except fid are optional. Count may take the forms: n: read n values into a column array inf: read to end of file, data is a column array [m,n]: read enough data to fill a matrix of size [m,n], matrix is filled in column order, and padded with zeros if insufficient data to fill it. m must be a positive integer, n may be inf – read to end of file, data has m rows, and however many columns are required.

valid precisions MATLABC or FortranInterpretation 'schar' 'signed char'Signed character; 8 bits 'uchar' 'unsigned char' Unsigned character; 8 bits 'int8''integer*1'Integer; 8 bits 'int16''integer*2'Integer; 16 bits 'int32''integer*4'Integer; 32 bits 'int64''integer*8'Integer; 64 bits 'uint8''integer*1'Unsigned integer; 8 bits 'uint16''integer*2'Unsigned integer; 16 bits 'uint32''integer*4'Unsigned integer; 32 bits 'uint64''integer*8'Unsigned integer; 64 bits 'float32''real*4'Floating-point; 32 bits 'float64''real*8'Floating-point; 64 bits 'double''real*8'Floating-point; 64 bits

Writing Binary Data >> count = fwrite(fid,data,precision) >> count = fwrite(fid,data,precision,skip) Data is written to the file in column order.

Reading & Writing Formatted Ascii Data To write formatted data: >> count = fprintf(fid,format,data,...) To read formatted ascii data: >> data = fscanf(fid,format) >> [A,count] = fscanf(fid,format,size) format is a string specifying the ascii data format, same as used in ‘C’ fscanf differs from its ‘C’ equivalent in that it is vectorized – multiple reads of format string carried out until end of file is reached, or matrix size is reached.

Format strings The format string consists of ordinary characters, and/or conversion specifications indicating data type: %12e Initial % character Field width Conversion character

Add one or more of these characters between the % and the conversion character: An asterisk (*)Skip over the matched value. If %*d, then the value that matches d is ignored and is not stored. A digit stringMaximum field width. For example, %10d. A letterThe size of the receiving object, for example, h for short, as in %hd for a short integer, or l for long, as in %ld for a long integer, or %lg for a double floating- point number.

%cSequence of characters; number specified by field width %dBase 10 integers %e, %f, %gFloating-point numbers %iDefaults to base 10 integers. Data starting with 0 is read as base 8. Data starting with 0x or 0X is read as base 16. %oSigned octal integer %sA series of non-white-space characters %uSigned decimal integer %xSigned hexadecimal integer [...]Sequence of characters (scanlist)

Reading whole lines >> tline = fgetl(fid) reads the next whole line of text from fid, returning it without the line termination character. >> tline = fgets(fid) reads the next line of text, including line termination character. >> tline = fgets(fid,nchar) reads nchar characters from the next line of fid, next read will start on next line.