CS 211 Lesson 24

Binary (unformatted) File Input/Output

Quote:

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

Lesson Objectives:

Lesson:

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')
fclose(File_id);

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')
fclose(File_id);

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
load('filename')
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