How to read and store binary data from a file and store it into the MATLAB in row major using fread function?

2 views (last 30 days)
I'd like to read in the binary file and generate 2 columns of data as shown in the text file.
The binary data consists of 16 bit signed integers from two channels of A/D data. The samples in the binary file alternate by channels, and it would be nice to turn the data into an array of two columns for the two channels.
For example consider a file contains following data:
1 2 3 4 5 6 7 8
 I would like to read data as:
1     2
3     4
5     6
7     8

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jun 2017
MATLAB always stores data into column major. But as workaround to this issue you can read the file as 2 rows and multiple columns and transpose the matrix to get expected result.
For example the file can be read as:
fileID = fopen('test1.bin');
A = fread(fileID,[2 inf],'int16');
B = transpose(A);
fclose(fileID);

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!