Reading binary files in MATLAB

Source: Internet
Author: User
I. Basic Knowledge
Source: http://crystaldonna.blog.sohu.com/84162722.html

From the file encoding method, files can be divided into two types: ASCII code files and binary code files:
An ASCII file is also called a text file. When a file is stored on a disk, each character corresponds to one byte and is used to store the corresponding ASCII code. The ASCII code file can be displayed by characters on the screen. For example, the source code file is an ASCII file, and the doscommand type can be used to display the file content. Because it is displayed by characters, you can read the file content.
Binary files are stored in binary encoding. For example, the storage format of hundreds of thousands is: 5678 00010110 occupies only two bytes. Although a binary file can be displayed on the screen, its content cannot be understood. When processing these files, the C system treats them as byte streams and does not differentiate the types. The start and end of the input and output streams are only controlled by the program, not by physical symbols (such as carriage returns. Therefore, this type of file is also called a "streaming File ".
Stream can be divided into two types: Text Stream and binary stream. The text stream can contain up to 255 characters, and the carriage return/line feed is converted to the line break "/N". (If you open a file in "text" mode, when reading characters, the system converts all the "/R/N" sequences into "/N ", convert "/N" to "/R/N" when writing "). The binary stream is non-explanatory. It processes one character at a time and does not convert characters.

Note:

/N is usually translated into "the end of a row", that is, LF (line-feed)
/R will be translated into "enter", that is, Cr (cariage-return)
For new lines of text files, it is generally represented by/N (LF) on UNIX, And/R (CR) on Mac,
Windows is represented by/n/R (CR-LF.

Whether to store binary files or text files in MATLAB depends on the fopen method. If wt is used, the files are stored as text files, which can be normally displayed after being opened in notepad; if W is used, it will be stored as a binary file. In this case, a small black box will appear when you use NotePad to open it. To display it normally, you can open it using tools such as WordPad or ultraedit.

2. Reading binary files in MATLAB
Source: http://blog.sina.com.cn/s/blog_4a0e9d52010091se.html

    When you are a beginner in MATLAB, you will always encounter the problem of reading binary data. The solution is given below. MATLAB can directly read binary data files and add them to the matrix. If you are familiar with C, you should be familiar with fopen, fclose, ftell, fseek, fread, fwrite, and feof functions, fortunately, these functions can still be used in MATLAB to read experiment data.   Assume that there is a data file named data. DAT: The first two K of data is stored as parameters. We need to skip this step during data processing. The following data is of the 16-digit integer type, with 512 data records in each group. Now we want to read all the data in this data file into an nx512 matrix. The number of N is variable, depending on the data in the data file. Write as follows using the. m Script: % deal data from specified data file
Clear;
Data_fname = 'data. dat '; % file name
Jump_distance = 2048;     % Here is the number of bytes skipped % open mode is binary open, in fact, 'R' is the line, Matlab is the default binary form to open the file file_id = fopen (data_fname, 'rb '); % skip jump_distance bytes starting from the file
Fseek (file_id, jump_distance, 'bof'); % create a matrix of 1x512 size manually
Raw_data = [];
While feof (file_id) = 0       % This is the top-level fread. The data type is int16, and 512 entries are read each time.     % Raw_array is a matrix of 512x1 each time, and ele_count is the number of reads (normally)
    [Row_array, ele_count] = fread (file_id, 512, 'int16 ');
    If ele_count <512 % Elecount <512 indicates that the data is insufficient and the end of the file is reached.
            Break;
    Else               % Transpose the row_array of X 1 to the matrix of 1x512
            Row_array = row_array' ;               % Then, append row_array to raw_data.
            Raw_data = [raw_data; row_array];
    End
End
% Get off the first line [] % here is the row of data manually constructed by the first line of raw_data. The rest is the data in the file.
Raw_data (1, :) = []; % close the file
Fclose (file_id); % delete other usless vars % here is to delete all the variables used, so that nothing in the workspace is messy. This is a good habit.
Clear data_fname jump_distance file_id FID ele_count I m n row_array ans;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.