string equivalent of fread

17 views (last 30 days)
William Sampson
William Sampson on 13 Sep 2012
Hi -
I can use
rawData = fread(fid,[59 59])
to read 59 bytes from a binary file, 59 times, convert to decimal, and put results into a 59 X 59 array.
I want to do the same, but read each hex byte literally into a string, i.e. 01 FF -> '01' 'FF', keeping the 59 X 59 structure (i.e. - 1st row: 1st 59 bytes in file as strings, keeping leading zeros; 2nd row: next 59 bytes in file as strings, keeping leading zeros; etc...)
The idea is that I want to combine some columns of strings into longer strings, convert to hex, then convert the hex to decimal.
. . ."AB" "01" . . . -> "AB01" -> AB01 -> 43777 (may not need all steps)
. . ."CC" "02" . . . -> "CC02" -> CC02 -> 52226
etc..
unknown number of rows, where "AB" and "CC" are in column 8, and "01" and "02" are in column 9.
Thoughts?

Accepted Answer

Jan
Jan on 13 Sep 2012
Edited: Jan on 13 Sep 2012
You cannot use FREAD to read a byte from a file and convert it to something like '01' of 'FF'. FREAD reads binary data, but the conversion to a HEX string is a kind of high-level operation. This is a job for FSCANF.
Or read bytes and convert them afterwards:
data = fread(fid, [59,59], '*uint8');
xdata = sprintf('%.2x', data); % And a RESHAPE on demand.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!