string equivalent of fread

11 次查看(过去 30 天)
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?

采纳的回答

Jan
Jan 2012-9-13
编辑:Jan 2012-9-13
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by