MATLAB: Open a hex file

hex fileopen

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

Best Answer

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);