I use the following code to open a hex file (please look at the attachment).
fid = fopen('FileName');
A = fread(fid);
My problem is instead of getting A as a cell containg n*1 (n is the number of rows in the hex file) I get one row of numbers. I would appreciate it if you help me get a result like below:
00 00 00 00 49 40 40 0D
03 00 30 43 C6 02 00 00
A3 6B 74 23 90 47 E4 40
and so on

댓글 수: 1

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 16일
Can you post the result you've got? maybe with simple use of reshape function you can get the expected result

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2013년 10월 16일
편집: Jan 2013년 10월 16일

5 개 추천

There is not format you can call "hex file". You can interpret a file as stream of bytes and display them by hex numbers. But from this point of view, any file is a hex file.
Please try this:
fid = fopen('FileName');
A = fread(fid, Inf, 'uint8');
fclose(fid);
Fmt = repmat('%02X ', 1, 16); % Create format string
Fmt(end) = '*'; % Trailing star
S = sprintf(Fmt, A); % One long string
C = regexp(S, '*', 'split'); % Split at stars
Perhaps a string with line breaks is sufficient:
Fmt = repmat('%02X ', 1, 16); % Create format string
Fmt(end:end+1) = '\n';
S = sprintf(Fmt, A);

댓글 수: 3

Ronaldo
Ronaldo 2013년 10월 16일
When the file is large, I get the following error for line 2:
OUT of MEMORY
Jan
Jan 2013년 10월 16일
@Ronaldo: Please specify "large". Some people in the forum use this for 1000 elements, some for 1e15 elements.
When importing the file exhausts too much memory, install more memory, use a 64 bit version of Matlab and OS, free arrays, which are not used anymore. You can import the data by fread(fid, Inf, '*uint8') also, which occupies just an 8.th of the memory. But for SPRINTF a modern Matlab version is required then, because older ones did not accept UINT8 as input.
Ronaldo
Ronaldo 2013년 10월 17일
The file that I want to open is 5 GB. I am using a 64 bit version of MATLAB and OS. Available physical memory of my computer is 31.2 GB. I was wondering whether there is anyway that I can prevent OUT of MEMORY problem if I want to open the 5 GB file.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 16일
편집: Azzi Abdelmalek 2013년 10월 16일

1 개 추천

fid = fopen('file.txt');
A = textscan(fid,'%s %s %s %s %s %s %s %s')
fclose(fid)
A=[A{:}]
arrayfun(@(x) strjoin(A(1,:)),(1:3)','un',0)

댓글 수: 8

Walter Roberson
Walter Roberson 2013년 10월 16일
Use '%x%x%x%x%x' instead of %s if the numeric form is desired.
Ronaldo
Ronaldo 2013년 10월 16일
편집: Ronaldo 2013년 10월 18일
The extension of the original file is not txt.
Ronaldo
Ronaldo 2013년 10월 16일
편집: Ronaldo 2013년 10월 16일
If I use the following code for the problem, I get the result that I want
fid = fopen('FIleName');
a = fread(fid);
for i=1:size(a,1)
sprintf('%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X',a(8*i-7:8*i,1))
end
BUT if the file size is large then this code is not working. This is the reason why I want "a" to be (n*1 cell which each cell contains one row of the image attached to the question) instead of a complete row or column. of all the elements in the hex file.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 16일
편집: Azzi Abdelmalek 2013년 10월 16일
Post the first samples you've got with
A = fread(fid);
Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 16일
If the data you've got are like:
A={'00' ;'00' ;'00';'00'; '49'; '40' ;'40' ;'0D'; '03'; '00' ;'30' ;'43'; 'C6'; '02'; '00' ;'00'}
You can make some transformations
out=reshape(A,8,[])'
result=arrayfun(@(x) strjoin(out(x,:)),(1:size(out,1))','un',0)
Ronaldo
Ronaldo 2013년 10월 16일
편집: Ronaldo 2013년 10월 16일
It is a column of double numbers. If I use dec2hex for each element of the column, I get the equivalent value in the original hex file. As you can see in the attachment the hex file contains different rows, I like "a" also contains several rows to avoid memory leak!
Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 16일
편집: Azzi Abdelmalek 2013년 10월 16일
Decimal numbers? please edit your question and make it as clear as possible
Jan
Jan 2013년 10월 16일
@Ronaldo: A memory leak? I assume, you mean something completely different.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

질문:

2013년 10월 16일

댓글:

2013년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by