CS 211 Lesson 24

Binary (unformatted) File Input/Output


Some succeed because they are destined to; most succeed because they are determined to.  Unknown

Lesson Objectives:


I. MATLAB Concepts

A. Saving/Loading Workspace Variables

stores the contents of the variables Alpha and Beta to a file called mydata.mat

B. Binary File I/O - fwrite and fread

File_id = fopen('mydata.dat', 'w');
fwrite(File_id, Alpha, 'double')

stores the contents of the variable Alpha to a file called "mydata.dat" in double precision format (8 bytes per value).

File_id = fopen('mydata.dat', 'r');
Alpha = fread(File_id, 5, 'int32=>double')

reads 5, 32-bit integers from the file mydata.dat and stores the values as double precision numbers into the variable Alpha

C. Commands vs. Functions (unrelated to file I/O)

load('filename', 'X', 'Y', 'Z')
load('filename', '-regexp', exprlist)
load('-mat', 'filename')
load('-ascii', 'filename')
S = load(...)

function format

load filename -regexp expr1 expr2 ...

command format

II. Good Programming Practices

III. Algorithms


Lab Work: Lab 24

References:  Chapman Textbook: sections 8.2, 8.5