read binary file into matrix

32 views (last 30 days)
Duncan
Duncan on 4 Dec 2012
I have a binary file, that is encoded as hex. If I use fread on it I get a 8192x1 double with values like these:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 69 48 48 50 50 69 55 50 54 57 09
these are hex values, so 69 is E for example. Now at the end there is an 09 (tab) and I would like that tab to indicate to go to the next cell in a matrix, so that when I read and process the file, I have all the data stored in a matrix. Now if this were a tab-indented file, I would just use importdata(...), but as it is in a binary, that I have to convert to a string, and then to a matrix, I can't figure out how to do it. Any ideas?

Answers (1)

Muthu Annamalai
Muthu Annamalai on 4 Dec 2012
Edited: Muthu Annamalai on 4 Dec 2012
Pay attention to the 'source' and 'precision' options to fread().
>> doc fread
is your friend
% Create the file
fid = fopen('magic5.bin', 'w');
fwrite(fid, magic(5));
fclose(fid);
% Read the contents back into an array
fid = fopen('magic5.bin');
m5 = fread(fid, [5, 5], '*uint8');
fclose(fid);

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!