How to import hex values from text file?
이전 댓글 표시
Hi, I have a text file of hex values in the format of '3D 3D 3D 3E 41' etc.. and want to import to a matrix in order to use inshow(Matrix) to reconstruct the image of the data. It is 1280 values in a row and 1024 columns (1280px X 1024 px image). I have tried fscanf and dlmread but they are not working as I expect. dlmread is not recognizing the first value and fscanf does not read the data in the way that I want when I use [1280 1024]. Examples below:
3D 3D 3D 3C 3E 3A 40 3C 3E 3C 3D 3B 3B 3C 3D 3C 3C 3C 3C 3D 3D 3B 3B 3D 3F 3B 3D 3E 3D 3C 3F 3C 3D 3C 3E 3E 3F 3C 40 3E 40 3D 3E 3D 3E 3D 3E 3B 3C 3B 3D 3D 3D 3D 40 3B 3D 3D 42 3D 3C 3B 3D 3C 3D 3B 3C 3B 3B 3C 3B 3E 3F 3D 3E 3D 3E 3E 40 3C 3E 3D 3D 3C 3D 3C 3D 3D 3F 3B 3A 3B 3C 3D 3F 3B 3C 3D 3D 3B 3B 3A 3B 3C 3B 3B 39 3B 3C 3C 3B 3D 3E 3D 3D 3A 3C 3D 3F 3D 3D 3D 3B 3C 3E 3A 3C 3E 3D 3B 3D 3B 3E 3C 3B 3D 3C 3C 3C 3A 3D 3C 3C 3B 3D 3C 3B 3C 3B 3C 3C 3C 3D 3C 3D 3D 3A 3B 40 3C 3D 3B 3E 3D 3B 3A 3D 3A 3E 3E 3D 3B 3E 3D 3C 3B 3E 3B 3E 3E 3D 3D 3B 3A 3E 3C 3D 3C 3D 3C 3B 3A 3E 3A 3C 3D 3C 3E 3D 3E 3D 3D 3D 3B 3B 3A 3D 3A 3C 3B 3D 38 3A 39 3B 3B 3E 3A 3D 3D 3D 3A 3C 3C 3E 3A 3D 3A 3C 3A 3B 38 3C 3A 3D 3C 3D 3B 39 3B 3A 3B 3D 3A 3B 39 3F 3A 3E 3C 3D 3B 3B 3C 38 39 3A 39 3B 39 3D 3C 3A 3A 3E 3B 3D 3B 3E 3B 3D 3D 3A 3B 3E 3E 3E 39 3E 3B 3A 39 3E 3C 39 39 3D 39 3C 3A 38 3A 3A 3E 3B 3B 3C 3B 3A 3A 3C 3B 3D 3B 3D 3B 3A 39 3C 3A 3C 3C 3A 3A 3B 3A 3C 38 3D 3C 3C 3A 3B 3A 3B 3B 3B 3A 3D 3A 3B 3A 3B 3C 3B 38 3B 39 3B 39 3E 39 3C 3B 3A 3B 3B 39 3C 3C 3A 3A 3A 3A 3A 38 3E 39 3C 3B 3D 3E 3C 39 39 39 3E 3B 3C 3A 39 39 3B 37 39 3B 3C 3C 3D 37 3A 3A 38 3C 3B 3A 3A 3A 3A 39 39 39 3C 38 3C 3B 3D 3C 39 3B 3C 3A 3A 3B 3C 3A 3A 3A 3A 37 3B 38 39 39 39 3A 39 39 39 3A 3A 3B 3C 3B 3D 39 3E 3A 3B 39 3A 3B 3C 3C 3C 37 39 3A 3C 35 3B 39 39 39 39 3B 3C 3A 39 3A 3A 3C 3D 3A 3C 3A 3B 3C 3C 3A 3B 3B 39 39 39 3C 3D 3A 3A 39 3D 3B 3B 3A 3A 38 37 3A 39 3A 3A 37 3A 3A 3B 37 3A 3A 3A 37 3D 3A 3C 39 3D 38 3C 37 3A 39 3B 3A 3B 3A 3C 3A 3B 39 3C 3C 3A 3A 39 39 38 3A 3A 3A 3B 38 37 3B 39 39 3A 39 37 3B 3A 3A 39 38 39 39 3C 3A 3A 3A 3A 38 3B 3C 38 3A 39 3A 3A 3B 38 38 39 3A 3B 3A 39 38 3A 39 3B 36 3A 37 3A 3A 3A 38 39 37 38 38 3B 38 3B 39 3B 38 3A 38 37 36 3B 38 39 3B 39 3A 39 39 3A 3B 3C 38 3A 38 38 39 38 39 3A 39 39 39 3B 3B 3D 39 39 38 38 38 3A 37 39 39 38 38 3B 39 3A 38 38 34 39 36 39 38 39 38 39 38 3A 37 39 37 39 35 38 37 37 39 39 37 38 38 38 39 38 37 37 37 39 39 38 37 39 39 39 37 3A 37 39 38 3A 36 38 38 39 39 3A 37 3C 39 39 39 36 37 36 37 37 37 37 37 37 37 37 37 39 37 37 37 3B 36 38 36 39 36 39 38 38 38 39 34 37 38 3A 37 38 39 3A 38 39 36 39 37 37 38 3A 37 39 39 3B 38 39 38 37 36 37 37 38 36 39 36 36 35 36 36 37 35 36 36 37 35 36 37 37 36 3A 37 36 35 35 37 36 37 37 38 36 35 35 36 37 35 37 37 38 38 37 39 3A 35 37 36 39 37 38 37 36 35 37 35 35 35 37 35 37 38 37 35 37 36 37 34 3A 34 36 39 3A 37 37 36 38 34 35 35 36 34 37 36 34 37 36 35 37 34 35 34 36 37 38 35 35 35 34 34 38 37 35 36 37 33 35 32 33 34 36 34 36 37 35 34 35 34 36 35 34 34 37 36 35 35 34 33 37 34 35 32 35 35 38 33 35 34 34 33 38 34 32 34 34 32 36 34 36 33 35 35 33 33 36 31 36 33 36 34 34 33 36 31 33 33 35 33 34 33 36 34 33 33 37 33 35 34 34 34 34 31 33 32 34 33 34 33 35 33 34 31 32 33 33 33 35 35 34 32 34 32 34 34 30 32 32 30 34 32 34 31 33 33 34 30 34 32 33 33 32 32 31 31 33 32 33 31 34 32 2F 33 32 2F 32 31 33 2F 34 32 33 31 32 2E 30 31 32 31 31 31 34 31 34 32 32 30 33 2E 31 2F 31 30 33 31 31 30 31 30 30 30 32 31 33 31 32 2F 30 30 32 31 2F 2E 30 30 32 30 31 2F 30 2F 31 30 32 2F 2F 32 31 2F 2F 2F 31 2F 30 2E 2F 2F 2F 30 30 30 30 2F 2D 2F 30 2C 2E 2C 30 2D 2F 2F 31 2F 2F 2E 2F 2E 2E 2F 31 31 2F 2C 30 2F 31 2C 30 31 2D 2D 2E 2F 2D 2D 2D 2D 2F 2D 2E 2B 2E 2D 2D 2D 2E 2D 2E 2C 2A 2D 2E 2E 2D 2C 2D 2B 2C 2E 2C 2B 2D 2A 2A 2D 2C 2B 2E 2B 2B 29 2D 2B 2B 2B 2D 2B 2B 2C 2C 2D 2B 2C 2C 2A 2C 2B 2C 2C 2C 2A 2B 29 2B 2A 2B 29 2A 29 2A 29 2C 2A 2A 27 2B 27 2C 28 2A 29 2B 29 2B 2A 29 2B 2A 2B 2A 28 2A 2B 29 2A 2A 27 2A 27 2A 2B 2B 28 2A 28 29 27 29 28 2A 27 29 28 28 28 27 29 29 26 27 29 2A 28 2A 28 29 26 28 27 28 26 27 26 2A 28 28 27 26 28 28 26 28 2A 27 27 27 28 27 27 26 27 26 27 27 27 29 25 27 25 27 24 26 26 28 28 26 25 27 24 28 25 27 25 25 25 26 24 26 24 26 25 25 24 25 24 25 22 23 25 24 23 25 23 25 24 23 22 23 22 25 21 24 23 23 22 23 22 22 22 25 21 24 23 23 21
3D 3D 3D etc...
This is one row of the data in hex. A fair few, say 20, spaces follow the end and another begins right below.
fscanf I was using:
fid = fopen('cb130.dat');
cb130mat = fscanf(fid,'%x');
fclose(fid);
dlmread I was using:
cb130mat = dlmread('cb130.dat');
This is the error that showed:
Mismatch between file and format string. Trouble reading number from file (row 1u, field 1u)
Thanks for the help.
댓글 수: 13
Stephen23
2015년 8월 6일
Please upload your file using the paperclip button and then clicking both the Choose file and Attach file buttons.
Walter Roberson
2015년 8월 6일
With the test data you show here, the '%x' format works. We will need a sample file to test with.
You will not be able to use dlmread for this: dlread() can only handle numeric values.
dpb
2015년 8월 6일
편집: Walter Roberson
2015년 8월 6일
But the data are numeric, Walter!!! :)
The limitation on dlmread is more restrictive than just "numeric"; it's also base 10. (Pedant mode off) <vbg>
Walter Roberson
2015년 8월 7일
Looking at the hex converted to decimal, I doubt the data is "numeric"; it looks to me to be either character or graphic represented as numeric.
dpb
2015년 8월 7일
What the data represent is immaterial to the point I was making (albeit it is somewhat pedantic, hence the smile/grin/chuckle) that if the %X format string works it can be considered numeric, just in base 16. Of course, all data are numeric in memory representation; it's only our interpretation of them that makes them otherwise...
I've wondered in the past why there wasn't more flexibility in some of the routines such as dlmread although there are ways around them it would sometimes be convenient to be slightly more general than they are...
Walter Roberson
2015년 8월 7일
But pedantically, the data is not numeric, the data is represented by something that represents something that represents numeric values (and I suspect there is at least one more another layer of representation in there too.)
Cedric
2015년 8월 7일
I wanted to mention this Wiki about the DIKW pyramid before I pull in the discussion a philosopher of science from the department next door.
dpb
2015년 8월 8일
And all this to an engineering major who thought Moby Dick was just a good story of a fishing trip ... :)
Walter Roberson
2015년 8월 8일
No-one knows; no-one has actually read Moby Dick, only skimmed some of the pages. (Except, of course, Laurie Anderson)
dpb
2015년 8월 9일
From the comments, looks like her reading may have been even more cursory... ;) I think it was roughly age 14 I first read it; can recall at least once more in HS and then the frosh American Lit prof who didn't much appreciate my trying to cast into a Swift-like satirical commentary... :)
Samuel Davies
2015년 8월 12일
Cedric
2015년 8월 12일
My code should work on your file.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
