open a hex file

40 visualizaciones (últimos 30 días)
Ronaldo
Ronaldo el 16 de Oct. de 2013
Comentada: Ronaldo el 17 de Oct. de 2013
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 comentario
Azzi Abdelmalek
Azzi Abdelmalek el 16 de Oct. de 2013
Can you post the result you've got? maybe with simple use of reshape function you can get the expected result

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 16 de Oct. de 2013
Editada: Jan el 16 de Oct. de 2013
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 comentarios
Jan
Jan el 16 de Oct. de 2013
@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 el 17 de Oct. de 2013
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.

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 16 de Oct. de 2013
Editada: Azzi Abdelmalek el 16 de Oct. de 2013
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 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 16 de Oct. de 2013
Editada: Azzi Abdelmalek el 16 de Oct. de 2013
Decimal numbers? please edit your question and make it as clear as possible
Jan
Jan el 16 de Oct. de 2013
@Ronaldo: A memory leak? I assume, you mean something completely different.

Iniciar sesión para comentar.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by