
Description
In numerical simulation and signal processing, data access primarily relies on txt and dat files, with reading being more troublesome than writing (for writing, fwrite and fprintf are generally sufficient). Therefore, the following discussion focuses on “reading txt files” (dat files are similar to txt files).
1. Difference Between Binary Files and Text Files:
When viewing a file as a series of bytes, the highest bit of each byte in a text file is 0, meaning that text files use seven bits of a byte to represent all information, while binary files use all bits in the byte. This is the key difference; the second question is what differences arise when opening files in text mode versus binary mode?
In fact, whether it is a binary file or a text file, it is just a series of 0s and 1s, but the way they are opened differs, leading to different treatments of these 0s and 1s. If opened in text mode, translation occurs, converting each byte to ASCII code, while in binary mode, no translation occurs; finally, text and binary files differ in how they are edited.
For instance, when editing text in Notepad, the smallest unit of editing is a byte; whereas for binary files, the smallest unit is a bit, and of course, we generally do not edit binary files manually.
From the perspective of file encoding, files can be divided into ASCII files and binary files:
ASCII files, also known as text files, store each character as one byte on disk, corresponding to its ASCII code. For example, the storage format of the number 5678 is:

Occupying a total of 4 bytes. ASCII files can display on the screen by character, for example, source program files are ASCII files, and can be displayed using the DOS command TYPE. Since they are displayed by character, the content can be understood.
Binary files are stored in binary encoding. For example, the storage format of the number 5678 is: 00010110 00101110, occupying only two bytes. Although binary files can also be displayed on the screen, their content is not understandable. The C system treats these files as character streams, processing them byte by byte. The start and end of input and output character streams are controlled by the program and not by physical symbols (such as carriage return). Therefore, these files are also referred to as “stream files”.
2. What is the Difference Between Text Mode and Binary Mode?
Streams can be divided into two types: text streams and binary streams. Text streams are interpretive, with a maximum length of 255 characters, where carriage return/new line is converted to the newline character “\n”, (if a file is opened in “text” mode, the system will convert all “\r\n” sequences to “\n” when reading characters, and convert “\n” back to “\r\n” when writing). Binary streams are non-interpretive, processing one character at a time without converting characters.
Note:
\n is generally translated by the operating system to mean “end of line”, i.e., LF (Line-Feed)
\r is translated to “carriage return”, i.e., CR (Carriage-Return)
For new lines in text files, UNIX generally uses \n (LF), Mac uses \r (CR), and Windows uses \r\n (CR-LF).
Typically, text streams are used to read and write standard text files, or to output characters to the screen or printer, or to accept keyboard input; while binary streams are used to read and write binary files (such as graphics or word processing documents), or to read mouse input, or to read/write modems. If a binary file is opened in text mode, “0D 0A” will automatically be converted to “\n” in memory. When writing, the process is reversed. However, files in Unicode/UTF/UCS formats must be opened and read/written in binary mode.
The above basics can actually be skipped, in short, for users: whether to store in binary or text file in MATLAB depends on the mode used in fopen; if using wt, it will be stored as a text file, which can be normally displayed when opened with Notepad; if using w, it will be stored as a binary file, which will show small black squares when opened with Notepad, and can be normally displayed using WordPad or UltraEdit.
1. Types of I/O Files Supported by Matlab (Corresponding to “Get/Store” Operations):(All file I/O programs do not require special toolboxes)
http://www.mathworks.com/support/tech-notes/1100/1102.html
(Note: From the table above, it can be seen that MATLAB does not support the storage of doc format documents (as doc documents contain many formatting control characters), please use txt or dat format instead)
2. I/O File Guide for Matlab:
http://www.mathworks.com/support/tech-notes/1600/1602.html
The following is part of the corresponding Chinese translation:
This technical support guide primarily deals with: ASCII, binary, and MAT files.
To obtain a complete list of functions available for reading and writing various file formats in MATLAB, you can type the following command:
help iofun
There are two types of file I/O programs in MATLAB: high level and low level.
High level routines: Include ready-made functions that can be used to read and write special format data, requiring only minimal programming.
Low level routines: Can accomplish relatively special tasks more flexibly, requiring more additional programming.
High level routines include ready-made functions that can be used to read and write special format data, requiring only minimal programming.
For example, if you have a text file containing numbers and letters that you want to import into MATLAB, you can either call some low level routines to write a function yourself or simply use the TEXTREAD function.
The key to using high level routines is: the files must be homogeneous, in other words, the files must have a consistent format. The following paragraphs describe some high level file I/O routines and provide examples to help understand the concept.
LOAD/SAVE
The main high level file I/O routines are LOAD and SAVE functions. LOAD can read MAT-file data or space-separated similar ASCII data. SAVE can write MATLAB variables into MAT-file format or space-separated ASCII data. In most cases, the syntax is quite simple. The following example uses numeric data from a space-separated ASCII file sample_file.txt:
1 5 4 16 8
5 43 2 6 8
6 8 4 32 1
90 7 8 7 6
5 9 81 2 3
Example:
Using LOAD and SAVE to read and write data
% Load the file into the matrix, M :
M = load(‘sample_file.txt’)
% Add 5 to M :
M = M +5
% Save M to a .mat file called ‘sample_file_plus5.mat’:
save sample_file_plus5 M
% Save M to an ASCII .txt file called ‘sample_file_plus5.txt’ :
save sample_file_plus5.txt M -ascii
UIGETFILE/UIPUTFILE
UIGETFILE/UIPUTFILE is based on a graphical user interface (GUI). It pops up a dialog box listing the files and directories in the current directory, prompting you to select a file. UIGETFILE allows you to choose a file to write (similar to Windows ‘Save As’ option?). With UIGETFILE, you can select an existing file to overwrite or enter a new file name. The return values of both functions are the selected file name and path.
Example:
Using UIGETFILE to select an M-file from the current directory
% This command lists all the M-files in the current directory and
% returns the name and path of the selected file
[fname,pname] = uigetfile(‘*.m’,’Sample Dialog Box’)
Note: UIGETFILE can only select one file at a time.
UIIMPORT/IMPORTDATA
UIIMPORT is a powerful and easy-to-use GUI-based high level routine for reading complex data files. The files must also be homogeneous.
IMPORTDATA forms the functionality of UIIMPORT without opening the GUI. IMPORTDATA can be used in functions or scripts, as the GUI-based file import mechanism is not ideal in functions or scripts. The following example uses a file ‘sample_file2.txt’ that contains several lines of file headers and text, numeric data:
This is a file header.
This file is an example.
col1 col2 col3 col4
A 1 4 612.000
B 1 4 613.000
C 1 4 614.000
D 1 4 615.000
Example: Using IMPORTDATA to read in a file with headers, text, and numeric data
% This reads in the file ‘sample_file2.txt’ and creates a
% structure D that contains both data and text data.
% Note the IMPORTDATA command specifies a white space
% as the delimiter of the file, but IMPORTDATA can usually
% detect this on its own
D = importdata(‘sample_file2.txt’,”) % Original text is incorrect?
D = importdata(‘sample_file2.txt’)
You can access the data and text fields in structure D to see the actual values in structure D, for example, input:
data = D.data
text = D.textdata
You can use UIIMPORT to read the same file and obtain the same structure.
Note: For ASCII data, you must verify that the import wizard correctly recognizes the column delimiter.
TEXTREAD/STRREAD
TEXTREAD is a powerful dynamic high level routine designed to read ASCII format text and/or numeric data files. STRREAD is similar to TEXTREAD except it reads from a string instead of a file.
Both functions can use many parameters to change their specific working methods, returning the specified output data read in. They effectively provide you with a “win-win” method, as they can read mixed ASCII and numeric data with one command (as high level routines do), and you can modify them to match your specific application (as low level routines do). Example:
Example 1: Using TEXTREAD to read in an entire file into a cell array
% This command reads in the file fft.m into the cell array, file
file = textread(‘fft.m’,’%s’,’delimiter’,’\n’,’whitespace’,”);
Example 2: Using STRREAD to read the words in a line
% This command uses the cell array created in Example 1 to
% read in each word of line 28 in ‘file’ to a cell array, words
words = strread(file{28},’%s’,’delimiter’,”)
Example 3: Using TEXTREAD to read in text and numeric data from a file with headers
% This command skips the 2 header lines at the top of the file
% and reads in each column to the 4 specified outputs
[c1 c2 c3 c4] = textread(‘sample_file2.txt’,’%s %s %s %s’,’headerlines’,2)
Example 4: Using TEXTREAD to read in specific rows of text and numeric data from a file
% This command reads in rows B and C of the file. The ‘headerlines’
% property is used to move down to the desired starting row and the
% read operation is performed 2 times
[c1 c2 c3 c4] = textread(‘sample_file2.txt’,…
‘%s %s %s %s’,2,’headerlines’,4)
Example 5: Using TEXTREAD to read in only the numeric data from a file containing text and numbers
% This command reads in only the numeric data in the file. The
% ‘headerlines’ property is used to move down to the first row
% of interest and the first column of text is ignored with the
% ‘*’ operator
[c2 c3 c4] = textread(‘sample_file2.txt’,’%*s %d %d %f’,’headerlines’,3)
DLMREAD/DLMWRITE/CSVREAD
DLMREAD and DLMWRITE functions can read and write delimited ASCII data, rather than using low level routines. They are easier to use than low level routines, with functionality that can be simplified into one line using DLMREAD/DLMWRITE instead of several lines with low level routines.
CSVREAD is used to read files where the delimiter is a comma, which is a special case of DLMREAD. DLMREAD is particularly useful when reading electronic spreadsheet files that are space and tab delimited. Taking ‘sample_file.txt’ as an example:
Example 1: Using DLMREAD to read in a file with headers, text, and numeric data
% This reads in the file ‘sample_file.txt’ and creates a matrix, D,
% with the numeric data this command specifies a white space as the
% delimiter of the file
D = dlmread(‘sample_file.txt’,”)
Example 2: Using DLMREAD to extract the first 3 columns of the last 3 rows
% This reads in the first 3 columns of the last 3 rows of
% the data file ‘sample_file.txt’ into the matrix, D_partial.
% Read the first 3 columns of the last 3 rows of the file ‘sample_file.txt’ into matrix D_partial.
D_partial = dlmread(‘sample_file.txt’,”,[2 0 4 2])
Example 3: Using DLMWRITE to write a comma delimited file
% This creates a file called ‘partialD.txt’ that consists of
% the first 3 columns of the last 3 rows of data where each
% element is separated by a comma
dlmwrite(‘partialD.txt’,D_partial,’,’)
Note: Ensure that DLMREAD and DLMWRITE specify that the index range starts from 0, not from 1.
WK1READ/WK1WRITE
WK1READ is used to read data from Lotus123 electronic spreadsheet files; WK1WRITE is used to write matrices to Lotus123 electronic spreadsheet files.
XLSREAD
XLSREAD is used to read numerical and textual data from Excel.