How do I create a for-loop that can read binary files of different/unknown sizes?

19 views (last 30 days)
Hi everybody! I am trying to read a lot of binary files of different sizes, using the fread command. I originally thought the binary files had the same dimensions but later found out, that this was not the case (so not a 8000 x 8000 matrix).
The first script i wrote was as following:
for i=1:length(a);
cell_name = a(i,1);
string_name = char(cell_name);
fileid1(i) = fopen(string_name,'r');
DN=fread(fileid1(i),[8000 8000],'uint16');
However, since the dimensions are not constant, I was wondering if there was a way to incorporate this into a for-loop (or something like that) so as to avoid having to type in the dimensions manually a couple of hundred times.
Any advice is greatly appreciated!
  2 Comments
James Tursa
James Tursa on 26 May 2015
Is this matrix the only data in the file? I.e., can you simply read in the entire file without specifying the size and then reshape the result?
Melissa Skou
Melissa Skou on 26 May 2015
Edited: Melissa Skou on 26 May 2015
Sadly, there are several other data sets in this file, thank you for the quick reply, though.

Sign in to comment.

Answers (1)

dpb
dpb on 26 May 2015
If there's only the one set of data been written to the file, then you don't need to know how big it is at all, simply use
data=fread(fid,inf,'uint8');
and you'll get the full file as a vector of N values. You can then reshape as desired, depending on what you know about the size. NB: Since Matlab stores in column-major order, you'll also need to transpose the array when reshape it to get into "normal" viewing order.
  2 Comments
Melissa Skou
Melissa Skou on 26 May 2015
Thanks for the answer! Unfortunately, there are several other data sets written to the file. I previously tried using the 'inf' but an error message occurred.
dpb
dpb on 26 May 2015
Well, then you needs must know the count in order to read the file. This surely was thought of when the file was written? The typical way this is handled is to write a header integer count first, then the data of that size.
If the originator of these files didn't do that or something similar, then you'll have to be able to retrieve that information from somewhere or you're stuck.
As to the original specific question asked, you can get the info from any source; it doesn't have to be entered manually as a constant; you can use a variable that is determined from some other source, but somewhere, somehow, you're going to have to come up with that information.
PS. BTW, if you know the first length, then you can use inf as the second argument in the size vector as above and return the array shape directly.
You can do things with the directory size value if you know something of the file type (as in the uint8 above), but again, you need enough information and prior knowledge for there to be some unique solution.
Now, if you can enter the values manually for each file, that implies you have that information from somewhere; so automate that piece of knowledge.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!