string equivalent of fread
조회 수: 18 (최근 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?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!