leer archivo binario en un matlab de matriz 2d

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 ?

preguntado el 08 de noviembre de 11 a las 16:11

2 Respuestas

Creo que esto es lo que necesitas:

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

respondido 08 nov., 11:20

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

respondido 08 nov., 11:20

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). - user574183

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 - Smash

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.