0

I have a text file that i need to read in that has different number of columns of hex data depending on the row. I am trying to read it all in and then manipulate the hex, but I can seem to find a good way to read the file in. I want to read in the first five columns before the hex (those will always be there) as strings, and then eventually manipulate the hex into decimal and plot. The number of hex words in each row is different though. I'm trying to use text scan but can figure out how to this. Any help would be greatly appreciated.

Here is some sample data:

x 0:47950 0x---- 001:00:07:56.63 16-R-08-04 0x8000 0x0012 0x0000

x 0:136994 0x---- 001:00:13:14.35 16-R-07-04 0x8000 0x0012 0x0000 0x0000

x 0:532637 0x---- 001:00:40:29.86 15-R-08-04 0x8000 0x0012 0x0000 0x0000 0x0000 0x0000

x 0:532637 0x---- 001:00:40:29.86 16-R-04-04 0x8000 0x0012 0x0000 0x0000 0x0000

x 0:47950 0x---- 001:00:07:56.63 16-R-08-04 0x8000 0x0012 0x0000

user2540100
  • 1
  • 1
  • 1

1 Answers1

1

This might get you pointed in the right direction.

Depending on the size of the file, fgetl() with string manipulations can be very slow, but it will probably work as a first pass.

fid = fopen('hex.txt');

approxNumLines = 10; % Pre-allocate for speed
realNumLines = 0; % Counter variable

resultingCellArray = cell(approxNumLines,1); % Pre-allocate for speed


line = fgetl(fid); % Get the first line
while line ~= -1
    realNumLines = realNumLines + 1;
    resultingCellArray{realNumLines} = strsplit(line);
    line = fgetl(fid);
end

resultingCellArray = resultingCellArray(1:realNumLines);

You will have to do some cell to hex to matrix operations after this, but that should be pretty straight forward depending on exactly how you want it.

horriblyUnpythonic
  • 853
  • 2
  • 14
  • 34