Input Output - PowerPoint PPT Presentation

About This Presentation
Title:

Input Output

Description:

Read tables of ASCII data with load. Other functions like ... line 1: mesh name. line 2: comment. line 3 : I R. I=node number (integer) R=value at node (double) ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 17
Provided by: andrewjp5
Category:
Tags: input | line1 | output

less

Transcript and Presenter's Notes

Title: Input Output


1
Input Output
Garbage In, Garbage Out
2
Outline
  • Announcements
  • Homework III due Wednesday
  • Advanced ASCII
  • Binary Basics
  • Filesystem Fun

3
Advanced ASCII
  • Read tables of ASCII data with load
  • Other functions like textread will read simple
    files
  • Sometimes, youve just got to do it yourself
  • Complicated files

4
Opening Files
  • To read a file manually, open with fopen
  • fidfopen(fname, rt) rt read text
  • fid will be lt1 if open fails
  • You should check and return an informative
    message
  • File I/O functions accept fid
  • Close the file when youre done with fclose(fid)

5
Reading files
  • Afscanf(fid,cstring,N)
  • like Cs fscanf, cstring is a C format string
  • d\tf--integer (d),tab(\t),double (f)
  • fscanf is vectorized and Matlab will keep
    trying to match cstring. Limit with N
  • linfgetl(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)

6
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
  • Inode number (integer)
  • Rvalue at node (double)
  • I have lots of these files to read (output from
    FORTRAN model)
  • function s2rread_s2r(fname)
  • fidfopen(fname,rt)
  • if(fidlt0)error(Unable to open ,fname)end

7
read_s2r I
  • Read line-by-line using fgetl
  • linfgetl(fid)Read meshname (ignore)
  • linfgetl(fid)Read comment (ignore)
  • j1
  • while(feof(fid))
  • linfgetl(fid)
  • s2r(j,)num2str(lin)
  • jj1
  • end

8
read_s2r II
  • Pre-allocate s2r ltopen and skip as beforegt
  • buf100size of buffer
  • s2rzeros(buf,2)lenbuf
  • j1
  • while(feof(fid))
  • if(jgtlen)
  • s2rs2r zeros(buf,2)add memory to s2r
  • lenlenbuf
  • end
  • linfgetl(fid)
  • s2r(j,)num2str(lin)
  • jj1
  • end
  • s2rs2r(1j-1,)remove trailing 0s

9
read_s2r III
  • Use fscanf
  • ltskip headers as beforegt
  • s2rfscanf(fid,df) s2r is 1-by-(2NN)
  • s2rI(1),R(1),I(2),R(2)
  • NNlength(s2r)
  • if(mod(NN,2)0)error(fname, contains )end
  • s2rreshape(s2r,2,NN/2)
  • s2r I(1) I(2) I(3) . . .
  • R(1) R(2) R(3) . . .
  • s2rs2rTranspose

10
Writing Files
  • Save matrices using save fname varname -ascii
  • Doing it yourself
  • fidfopen(fname,wt) wt write text
  • fprintf(fid,cstring, variables)
  • Example
  • A(110), sin(2pi0.1(110))integers,
    doubles
  • fidfopen(example.txt,wt)
  • fprintf(fid,d f\n,A)ensures first column
    is an integer
  • fclose(fid)

11
Binary Basics
  • All computer files are binary, that is composed
    of 0s and1s
  • 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 1s and 0s that correspond to
    the double precision value of pi, that takes only
    8 bytes

12
Problem with Binary Files
  • You cant just look at them
  • You must know exactly how they were created
  • integers vs. floating point
  • single precision vs. double precision
  • signed vs. unsigned

13
Reading Binary files
  • fidfopen(fname,r)r read binary
  • Afread(fid,N,precision)
  • Nnumber 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

14
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
  • youre too poor (or cheap) to buy a new hard drive

15
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

16
Example .mat from C program
  • 1. Include matlab C routines
  • include "matlab.h
  • 2. Create some arrays using C
  • double C10
  • for(j0jlt10j)Cj2.0j
  • 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
Write a Comment
User Comments (0)
About PowerShow.com