user574183
user574183

Reputation: 716

read binary file into a 2d array matlab

I use this code fragment to read a binary file into a array

 fid=fopen('data.bin','rb') % opens the file for reading
 A = fread(fid, count, 'int16') % reads _count_ elements and stores them in A

But it reads the file into a 1 dimensional array. Is there a direct method to read a binary file into a 2d array without me having to write loops to do that ?

Upvotes: 1

Views: 3910

Answers (2)

vharavy
vharavy

Reputation: 4991

I believe this is what you need:

fid = fopen('data.bin','rb');
A = fread(fid, [rows columns], 'int16')

Upvotes: 3

Amro
Amro

Reputation: 124563

You must know beforehand the number of rows/columns of the matrix you want to read. This way you read the values as an array, then reshape the result into the expected size:

A = reshape(A,[r c]);

Upvotes: 1

Related Questions