12
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 11”

Computer Simulation Lab

Embed Size (px)

DESCRIPTION

Computer Simulation Lab. “Lecture 11”. Electrical and Computer Engineering Department SUNY – New Paltz. Importing and exporting data. Objectives • the load and save commands; • low-level file I/O functions. Save Command. SAVE Save workspace variables to disk. - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Simulation Lab

SUNY-New Paltz

Computer Simulation Lab

Electrical and Computer Engineering Department

SUNY – New Paltz

“Lecture 11”

Page 2: Computer Simulation Lab

SUNY-New Paltz

Importing and exporting data

Objectives

• the load and save commands;

• low-level file I/O functions.

Page 3: Computer Simulation Lab

SUNY-New Paltz

Save Command

SAVE Save workspace variables to disk

SAVE FILENAME saves all variables to binary “filename.mat"

SAVE FILENAME X Y Z saves X, Y, and Z

SAVE ... -ASCII

Page 4: Computer Simulation Lab

SUNY-New Paltz

Examples

A =[1 2 3 ; 4 5 6]

save myData A

View myData.mat with a text editor

save myData -ascii

Page 5: Computer Simulation Lab

SUNY-New Paltz

Load Command

load myData

load myData.txt

A=load(‘myData.txt’)

Page 6: Computer Simulation Lab

SUNY-New Paltz

Low-level file I/O functionsSTART

Write

Open a file with a specific"file_name"for "write"

file handle is created

Type ofOperation

Read

Open a file with a specific"file_name"for "Read"

file handle is created

Write One record to the file(Automatic Pointer Update)

Read One record from the file(Automatic Pointer Update)

End ofOperation

End ofOperation

NoNo

Close THE file

Page 7: Computer Simulation Lab

SUNY-New Paltz

Low-level file I/O functions

fid = fopen(’marks.bin’, ’w’)

fwrite(fid, name)

fclose(fid)

fread(fid, 78, ’float’);

File Handle

Page 8: Computer Simulation Lab

SUNY-New Paltz

Low-level file I/O functions

fid = fopen(’marks.bin’, ’w’)

fwrite(fid, name)

Open for Write

Variable Name

Pointer

Update

Page 9: Computer Simulation Lab

SUNY-New Paltz

Low-level file I/O functions

fid = fopen(’marks.bin’, ’r’)

A=fread(fid, 16, ‘float’)

Open for Read

Variable Size&Type

Pointer

Update

Variable

Name

Page 10: Computer Simulation Lab

SUNY-New Paltz

Examples

1- Create a binary file named “odd.bin” that contains all the odd numbers between 1 and 99. Examine the file by text editor.

2- Open the binary file and read its contents into an array named OddNums.

3- Append the even numbers between 2 and 100 to the above file. Examine the file by text editor.

Page 11: Computer Simulation Lab

SUNY-New Paltz

MATLAB Code

fid=fopen('test1.bin','w')

x=[1:2:100];

fwrite(fid,x)

fclose(fid);

fid=fopen('test1.bin','r')

y = fread(fid)

fclose(fid);

fid=fopen('test1.bin','a')x=[2:2:100];fwrite(fid,x)fclose(fid);

fid=fopen('test1.bin','r')y = fread(fid)fclose(fid);

Page 12: Computer Simulation Lab

SUNY-New Paltz

Example

Create a text file named ‘ASCII.TXT’ that contains all the ASCII characters between 0 and 255. Use a text editor to examine the file.

fid=fopen('ASCII.TXT','w')

x=[1:255];

fwrite(fid,char(x))

fclose(fid);