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().
Type: double | integer
data
Matrix of values to be written.
Type: double | integer | char | string | logical | struct | cell
Dimension: vector | matrix
precision
String specifying the type of data to write.
Type: char | string
Valid precision types:
  • 'char' or 'char*1' or 'schar' or 'signed char' for a single character.
  • 'uchar' or 'unsigned char' for unsigned character.
  • 'ulong' or 'unsigned long' for platform dependent unsigned long integer.
  • 'uint' or'unsigned int' for platform dependent unsigned integer.
  • 'ushort' or 'unsigned short' for platform dependent unsigned short integer.
  • 'long' for platform dependent long integer.
  • 'int' for platform dependent integer.
  • 'short for platform dependent short integer.
  • 'uint64' for 64 bit unsigned integer.
  • 'uint32' for 32 bit unsigned integer.
  • 'uint16' for 16 bit unsigned integer.
  • 'uint8' for 8 bit unsigned integer.
  • 'int8' or 'integer*1' for 8 bit signed integer.
  • 'int16' or 'integer*2' for 16 bit signed integer.
  • 'int32' or 'integer*4' for 32 bit signed integer.
  • 'int64' or 'integer*8' for 64 bit signed integer.
  • 'float' for platform dependent single precision floating point number.
  • 'double' or 'float64' or 'real*8' for 64 bit floating point number.
  • 'single' or 'float32' or 'real*4' for 32 bit floating point number.
skip (optional)
Argument specifying the number of bytes to skip after writing each value. If unspecified, the value is 0.
Type: double | integer

Outputs

R
Number of values written to the file.
Type: double | integer

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