fwrite
Writes binary data to the file ID.
Syntax
R = fwrite(fileID, data, precision, skip)
Inputs
- fileID
- File ID returned from earlier call to fopen().
- data
- Matrix of values to be written.
- precision
- String specifying the type of data to write.
- skip (optional)
- Argument specifying the number of bytes to skip after writing each value. If unspecified, the value is 0.
Outputs
- R
- Number of values written to the file.
Example
Read example using fwrite and fread
functions:
% open file using 'w+'
fileID = fopen('testfile', 'w+')
% create a matrix
M = [ 1923.71288 4023.03575 9768.82832 9195.83701 104.13143 4261.35201 ];
% write matrix to file
fwrite_result = fwrite(fileID, M, 'double')
fflush_result = fflush(fileID)
printf('\n')
% return as much as possible in a column vector
frewind_result = frewind(fileID);
R1 = fread(fileID, inf, 'double');
printf('Matrix dimensions: columns %2d rows %2d\n', size(R1)(2), size(R1)(1));
% return 2 values in a column vector
frewind_result = frewind(fileID);
R2 = fread(fileID, 2, 'double');
printf('Matrix dimensions: columns %2d rows %2d\n', size(R2)(2), size(R2)(1));
% return as much as possible in 2 rows
frewind_result = frewind(fileID);
R3 = fread(fileID, [2, inf], 'double');
printf('Matrix dimensions: columns %2d rows %2d\n', size(R3)(2), size(R3)(1));
% return 2 entries in 1 row
frewind_result = frewind(fileID);
R4 = fread(fileID, [1, 2], 'double');
printf('Matrix dimensions: columns %2d rows %2d\n', size(R4)(2), size(R4)(1));
fclose_result = fclose(fileID)
fileID = 5
fwrite_result = 6
fflush_result = 0
Matrix dimensions: columns 1 rows 6
Matrix dimensions: columns 1 rows 2
Matrix dimensions: columns 3 rows 2
Matrix dimensions: columns 2 rows 1
fclose_result = 0