Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Introduction to M ATLAB 4 Useful stuff for real programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
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.
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.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Data Representation Kieran Mathieson. Outline Digital constraints Data types Integer Real Character Boolean Memory address.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
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.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Aloha Aloha What you see: What the computer sees: binary number columns binary number columns
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Matlab Training Session 10: Loading Binary Data Course Website: Training Sessions.htm.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CISC105 – General Computer Science Class 9 – 07/03/2006.
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)
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
1 Lab 1. C Introduction  C: –Developed by Bell lab. in –a procedure-oriented programming language.  Developing environments: –Editing –Preprocessing.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Copyright © – Curt Hill Types What they do.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Input Output Garbage In, Garbage Out. Outline Announcements: –Homework III: due Today. by 5, by Discuss on Friday. –Homework IV: on web, due following.
CSC 298 Streams and files.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Reading and Writing Data Files
How Computers Store Variables
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Computer Systems and Networks
What to bring: iCard, pens/pencils (They provide the scratch paper)
Chapter 2 part #3 C++ Input / Output
Lecture 13 Input/Output Files.
Matlab review Matlab is a numerical analysis system
פרטים נוספים בסילבוס של הקורס
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Matlab Training Session 5: Importing Data
funCTIONs and Data Import/Export
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Note on Indexing of Array Elements
Input/Output Functions
Signals.
Summary of what we learned yesterday
Chapter 2 part #3 C++ Input / Output
Input/Output Functions
Programming The ideal style of programming is Structured or
Presentation transcript:

Input Output Garbage In, Garbage Out

Outline Announcements: –Homework III: due Wednesday Advanced ASCII Binary Basics Filesystem Fun

Advanced ASCII Read tables of ASCII data with load Other functions like textread will read simple files Sometimes, you’ve just got to do it yourself –Complicated files

Opening Files To read a file manually, open with fopen –fid=fopen(‘fname’, ‘rt’); %rt= “read text” –fid will be <1 if open fails –You should check and return an informative message File I/O functions accept fid Close the file when you’re done with fclose(fid)

Reading files A=fscanf(fid,cstring,{N}) –like C’s fscanf, cstring is a C format string: ‘%d\t%f’--integer (%d),tab(\t),double (%f) –fscanf is “vectorized” and Matlab will keep trying to match cstring. Limit with N lin=fgetl(fid) –Reads a single line from the file as text (char array) –Process lin with str2num, findstr, sscanf Test for end of file with feof(fid);

Example: Reading.s2r file.s2r files are a simple text format for storing data defined on a 2D horizontal mesh Specification: –line 1: mesh name –line 2: comment –line 3+: I R I=node number (integer) R=value at node (double) I have lots of these files to read (output from FORTRAN model) function s2r=read_s2r(fname); fid=fopen(fname,’rt’); if(fid<0);error([‘Unable to open ‘,fname]);end

read_s2r I Read line-by-line using fgetl lin=fgetl(fid);%Read meshname (ignore) lin=fgetl(fid);%Read comment (ignore) j=1; while(~feof(fid)); lin=fgetl(fid); s2r(j,:)=num2str(lin); j=j+1; end

read_s2r II Pre-allocate s2r: buf=100;%size of buffer s2r=zeros(buf,2);len=buf; j=1; while(~feof(fid)); if(j>len); s2r=[s2r; zeros(buf,2)];%add memory to s2r len=len+buf; end lin=fgetl(fid); s2r(j,:)=num2str(lin); j=j+1; end s2r=s2r(1:j-1,:);%remove trailing 0’s

read_s2r III Use fscanf s2r=fscanf(fid,’%d%f’); %s2r is 1-by-(2*NN) %s2r=[I(1),R(1),I(2),R(2)…] NN=length(s2r); if(mod(NN,2)~=0);error([fname,’ contains …]);end s2r=reshape(s2r,2,NN/2); %s2r=[ I(1) I(2) I(3)... % R(1) R(2) R(3)...] s2r=s2r’;%Transpose;

Writing Files Save matrices using save fname varname - ascii Doing it yourself: –fid=fopen(‘fname’,’wt’); %wt= “write text” –fprintf(fid,cstring, variables) –Example: A=[(1:10)’, sin(2*pi*0.1*(1:10)’)];%[integers, doubles] fid=fopen(‘example.txt’,’wt’); fprintf(fid,’%d %f\n’,A’);%ensures first column is an integer fclose(fid);

Binary Basics All computer files are “binary”, that is composed of 0’s and1’s When the computer reads ASCII files, it takes chunks of 8 bits (1 byte) and looks up the character To save pi to 16 digits takes 18 bytes in ASCII If you save the 1’s and 0’s that correspond to the double precision value of pi, that takes only 8 bytes

You can’t just look at them You must know exactly how they were created –integers vs. floating point –single precision vs. double precision –signed vs. unsigned Problem with Binary Files

fid=fopen(fname,’r’);%’r’ = read binary A=fread(fid,N,precision) –N=number of data points, use Inf to read everything –precision is how the file was created “uint64” is an unsiqned integer saved in 64 bits “double” is a double Reading Binary files

Free advice (you get what you pay for) The only reasons to use binary files are –someone gives you one –you enjoy frustration and pain –you’re too poor (or cheap) to buy a new hard drive

Writing.mat files outside Matlab.mat files are a great format for storing data –easy to use with Matlab –multiple variables / file –compact It is possible to save data to.mat files from C/C++ programs using Matlab C/C++ library

Example:.mat from C program 1. Include matlab C routines –#include "matlab.h” 2. Create some arrays using C double C[10]; for(j=0;j<10;j++){C[j]=2.0*j;} 3. Call matlab C routines mlfEnterNewContext(0,0); /* Starts MATLAB-like memory management */ mlfSave(mxCreateString(”Cdata.mat"),"w","C", mlfDoubleMatrix(10,1,C,NULL), NULL); mlfRestorePreviousContext(0,0); 4. Compile & link to Matlab library mbuild mycprogram.c