|On this page…|
Low-level file I/O functions allow the most direct control over reading or writing data to a file. However, these functions require that you specify more detailed information about your file than the easier-to-use high-level functions. For a complete list of high-level functions and the file formats they support, see Supported File Formats for Import and Export.
If the high-level functions cannot export your data, use one of the following:
fprintf, which writes formatted data to a text or ASCII file; that is, a file you can view in a text editor or import into a spreadsheet. For more information, see Export to Text Data Files with Low-Level I/O.
fwrite, which writes a stream of binary data to a file. For more information, see Writing Binary Data to a File.
Use the fwrite function to export a stream of binary data to a file. As with any of the low-level I/O functions, before writing, open or create a file with fopen, and obtain a file identifier. When you finish processing a file, close it with fclose.
By default, fwrite writes values from an array in column order as 8-bit unsigned integers (uint8).
For example, create a file nine.bin with the integers from 1 to 9:
fid = fopen('nine.bin','w'); fwrite(fid, [1:9]); fclose(fid);
If the values in your matrix are not 8-bit unsigned integers, specify the precision of the values. For example, to create a file with double-precision values:
mydata = [pi, 42, 1/3]; fid = fopen('double.bin','w'); fwrite(fid, mydata, 'double'); fclose(fid);
For a complete list of precision descriptions, see the fwrite function reference page.
By default, fopen opens files with read access. To change the type of file access, use the permission string in the call to fopen. Possible permission strings include:
r for reading
w for writing, discarding any existing contents of the file
a for appending to the end of an existing file
To open a file for both reading and writing or appending, attach a plus sign to the permission, such as 'w+' or 'a+'. For a complete list of permission values, see the fopen reference page.
Note: If you open a file for both reading and writing, you must call fseek or frewind between read and write operations.
When you open a file, MATLAB creates a pointer to indicate the current position within the file. To read or write selected portions of data, move this pointer to any location in the file. For more information, see Moving within a File.
Create a file magic4.bin as follows, specifying permission to write and read:
fid = fopen('changing.bin','w+'); fwrite(fid,magic(4));
The original magic(4) matrix is:
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
The file contains 16 bytes, 1 for each value in the matrix. Replace the second set of four values (the values in the second column of the matrix) with the vector [44 44 44 44]:
% fseek to the fourth byte after the beginning of the file fseek(fid, 4, 'bof'); %write the four values fwrite(fid,[44 44 44 44]); % read the results from the file into a 4-by-4 matrix frewind(fid); newdata = fread(fid, [4,4]) % close the file fclose(fid);
The newdata in the file changing.bin is:
16 44 3 13 5 44 10 8 9 44 6 12 4 44 15 1
Add the values [55 55 55 55] to the end of the changing.bin file created in the previous example.
% open the file to append and read fid = fopen('changing.bin','a+'); % write values at end of file fwrite(fid,[55 55 55 55]); % read the results from the file into a 4-by-5 matrix frewind(fid); appended = fread(fid, [4,5]) % close the file fclose(fid);
The appended data in the file changing.bin is:
16 44 3 13 55 5 44 10 8 55 9 44 6 12 55 4 44 15 1 55
Different operating systems store information differently at the byte or bit level:
Big-endian systems store bytes starting with the largest address in memory (that is, they start with the big end).
Little-endian systems store bytes starting with the smallest address (the little end).
Windows® systems use little-endian byte ordering, and UNIX® systems use big-endian byte ordering.
To create a file for use on an opposite-endian system, specify the byte ordering for the target system. You can specify the ordering in the call to open the file, or in the call to write the file.
For example, to create a file named myfile.bin on a big-endian system for use on a little-endian system, use one (or both) of the following commands:
Open the file with
fid = fopen('myfile.bin', 'w', 'l')
Write the file with
fwrite(fid, mydata, precision, 'l')
where 'l' indicates little-endian ordering.
If you are not sure which byte ordering your system uses, call the computer function:
[cinfo, maxsize, ordering] = computer
The returned ordering is 'L' for little-endian systems, or 'B' for big-endian systems.
Encoding schemes support the characters required for particular alphabets, such as those for Japanese or European languages. Common encoding schemes include US-ASCII or UTF-8.
The encoding scheme determines the number of bytes required to read or write char values. For example, US-ASCII characters always use 1 byte, but UTF-8 characters use up to 4 bytes. MATLAB automatically processes the required number of bytes for each char value based on the specified encoding scheme. However, if you specify a uchar precision, MATLAB processes each byte as uint8, regardless of the specified encoding.
If you do not specify an encoding scheme, fopen opens files for processing using the default encoding for your system. To determine the default, open a file, and call fopen again with the syntax:
[filename, permission, machineformat, encoding] = fopen(fid);
If you specify an encoding scheme when you open a file, the following functions apply that scheme: fscanf, fprintf, fgetl, fgets, fread, and fwrite.
For a complete list of supported encoding schemes, and the syntax for specifying the encoding, see the fopen reference page.
The available precision values for fwrite do not explicitly support complex numbers. To store complex numbers in a file, separate the real and imaginary components and write them separately to the file.
After separating the values, write all real components followed by all imaginary components, or interleave the components. Use the method that allows you to read the data in your target application.
For example, consider the following set of complex numbers:
nrows = 5; ncols = 5; z = complex(rand(nrows, ncols), rand(nrows, ncols)); % Divide into real and imaginary components z_real = real(z); z_imag = imag(z);
One approach: write all the real components, followed by all the imaginary components:
adjacent = [z_real z_imag]; fid = fopen('complex_adj.bin', 'w'); fwrite(fid, adjacent, 'double'); fclose(fid); % To read these values back in, so that: % same_real = z_real % same_imag = z_imag % same_z = z fid = fopen('complex_adj.bin'); same_real = fread(fid, [nrows, ncols], 'double'); same_imag = fread(fid, [nrows, ncols], 'double'); fclose(fid); same_z = complex(same_real, same_imag);
An alternate approach: interleave the real and imaginary components for each value. fwrite writes values in column order, so build an array that combines the real and imaginary parts by alternating rows.
% Preallocate the interleaved array interleaved = zeros(nrows*2, ncols); % Alternate real and imaginary data newrow = 1; for row = 1:nrows interleaved(newrow,:) = z_real(row,:); interleaved(newrow + 1,:) = z_imag(row,:); newrow = newrow + 2; end % Write the interleaved values fid = fopen('complex_int.bin','w'); fwrite(fid, interleaved, 'double'); fclose(fid); % To read these values back in, so that: % same_real = z_real % same_imag = z_imag % same_z = z % Use the skip parameter in fread (double = 8 bytes) fid = fopen('complex_int.bin'); same_real = fread(fid, [nrows, ncols], 'double', 8); % Return to the first imaginary value in the file fseek(fid, 8, 'bof'); same_imag = fread(fid, [nrows, ncols], 'double', 8); fclose(fid); same_z = complex(same_real, same_imag);