1

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 ?

2 Answers 2

3

I believe this is what you need:

fid = fopen('data.bin','rb');
A = fread(fid, [rows columns], 'int16')
Sign up to request clarification or add additional context in comments.

Comments

1

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]);

2 Comments

my 1 D array length is 11139. How can i make use of reshape function. when i try to do B = reshape(A,100,111) i get a error Error using ==> reshape To RESHAPE the number of elements must not change. I cant even do a reshape(A,100,111.39) nor a reshape(A,100,112).
the problem is, as Amro said, that you need to know the expected rows/colums... if you're expecting 11100 elements, and you are reading more, then the problem comes from the way you read the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.