MATLAB Programming for Numerical Analysis by César Pérez López

MATLAB Programming for Numerical Analysis by César Pérez López

Author:César Pérez López
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


As a first example we can view the contents of the file fclose.m using the command type as follows:

>> type fclose.m

%FCLOSE Close file.

% ST = FCLOSE(FID) closes the file with file identifier FID,

% which is an integer obtained from an earlier FOPEN. FCLOSE

% returns 0 if successful and -1 if not.

%

% ST = FCLOSE('all') closes all open files, except 0, 1 and 2.

%

% See also FOPEN, FREWIND, FREAD, FWRITE.

% Copyright 1984-2001 The MathWorks, Inc.

% $Revision: 5.8 $ $Date: 2001/04/15 12:02:12 $

% Built-in function.

This is equivalent to using the command type before opening the file with fopen, followed by reading its contents with fread and presenting it with the function char.

>> fid = fopen('fclose.m','r');

>> F = fread(fid);

>> s = char(F')

s =

%FCLOSE Close file.

% ST = FCLOSE(FID) closes the file with file identifier FID,

% which is an integer obtained from an earlier FOPEN. FCLOSE

% returns 0 if successful and -1 if not.

%

% ST = FCLOSE('all') closes all open files, except 0, 1 and 2.

%

% See also FOPEN, FREWIND, FREAD, FWRITE.

% Copyright 1984-2001 The MathWorks, Inc.

% $Revision: 5.8 $ $Date: 2001/04/15 12:02:12 $

% Built-in function.

In the following example, we create a binary file id4.bin which contains the 16 elements of the identity matrix of order 4 stored in 4 byte integers (64 bytes in total). First we open the file which will contain the matrix, with permission to read and write, and then write the matrix to the file with the appropriate format. Finally, we close the open file.

>> fid = fopen ('id4. bin ',' w +')

fid =

5

>> fwrite(fid,eye(4),'integer*4')

ans =

16

>> fclose (5)

ans =

0

In the previous example, when the file was opened, the system assigned ID 5 to it. After writing the matrix to the file, it was necessary to close it with the command fclose using the indicator. The answer of zero means the closure has been successful.

If we now want to see the contents of the binary file just recorded, we open it, with reading permission, by using the command fopen and read its elements with fread, in the same matrix structure and format in which it was saved.

>> fid = fopen('id4.bin','r+')

fid =

5

>> [R,count]=fread(5,[4,4],'integer*4')

R =

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

count =

16



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.