how to read the data byte by byte in .raw file?

I have .raw file of size around 150MB and the data is stored in the form of hexadecimal , i want to read this data byte level and to display. I am giving here example of how data is stored in file.
00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00
00010 63 00 B5 00 21 00 A3 00 61 00 D7 00 5A 00 25 00
00020 ---------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------
I am not able to read this file , suggest me how to read this file and display the contents .
thank you.

Réponses (2)

Cris LaPierre
Cris LaPierre le 7 Mar 2021

0 votes

See the documentation for fread (Read data from binary file). There are some examples on that page that should help you get started.
For an alternate approach using textscan, see this answer:

2 commentaires

I have already gone through this textscan and fread but not able to load this file to new variable. Here the file extension is .raw format. Is there any command or function to convert.raw to.text/.csv format
The extension doesn't really matter. What's more important is the actual formatting inside the file.

Connectez-vous pour commenter.

if ispc()
filename = 'myfile.raw';
S = fileread(filename);
else
%get the data into MATLAB as we are not given a sample file
S = {'00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00'
'00010 63 00 B5 00 21 00 A3 00 61 00 D7 00 5A 00 25 00'
'00020 31 41 59 26 59 35'}; %illustrating ending short
S = strjoin(S, '\n');
end
S = regexprep(S, '^[0-9a-fA-F]{5}', '', 'lineanchors')
S =
' 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00 63 00 B5 00 21 00 A3 00 61 00 D7 00 5A 00 25 00 31 41 59 26 59 35'
bytes = uint8(sscanf(S, '%2x', [1 inf]))
bytes = 1×38
0 18 6 37 50 69 2 53 194 0 183 0 253 0 76 0 99 0 181 0 33 0 163 0 97 0 215 0 90 0
I changed the approach I was going to post involving textscan() in order to instead use a method that works even if the final line is not an exact multiple of 16 bytes. The textscan() approach I was going to use was fine at reading the data, but it padded the unused spaces on the line with 00, and even if I had changed that to a different constant there would have been the potential for problems. Besides, this approach works perfectly well.

8 commentaires

thank you, sorry for late reply. i can't post my data here because its confidential data, i am not getting exactly
filename = 'myfile.raw';
S = fileread(filename);
in this i want to read total .raw file to S as .csv file format and i want read from S for my display
filename = 'myfile.raw';
outfile = 'myfile.csv';
perrow = 25;
S = fileread(filename);
S = regexprep(S, '^[0-9a-fA-F]{5}', '', 'lineanchors');
bytes = uint8(sscanf(S, '%2x', [1 inf]));
[fid, msg] = fopen(outfile, 'w');
if fid < 0
error('failed to open file "%s" because "%s"', outfile, msg);
end
fmt = [repmat('%d,', 1, perrow-1), '%d\n'];
fprintf(fid, fmt, bytes(1:end-1));
fprintf(fid, '%d\n', bytes(end));
fclose(fid);
This reads the .raw file, discarding the position information such as the 00020 . It converts the hexadecimal. Then it writes out the information to the given file outfile, writing perrow across.
The bit about about only writing to end-1 the first time has to do with ensuring that no comma is output after the last value.
i am getting error like this
"array indices must be positive integers or logical values at this line
fprintf(fid, '%d\n', bytes(end));"
Here i am giving one example data file format , in this i want to read the hex data line by line except offset and lastpart. But this file is in .raw format with me.
Walter Roberson
Walter Roberson le 17 Mar 2021
Modifié(e) : Walter Roberson le 17 Mar 2021
That error would happen if bytes was empty.
You have indicated to us that the file contains data such as
00000 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00
which is a file that literally starts with '0' five times, then some spaces, then '0', '0', '', '1', '2' and so -- that everything you showed us is text in a file and the text happens to represent hexadecimal values that you want converted to decimal.
But now you show us something that has different colors and has fine ruler lines at the header and separating the parts. That cannot be a text file! The right side shows characters such as ü which are not part of same character set as the extended ascii line drawing characters. It is true that Unicode includes both https://en.m.wikipedia.org/wiki/Box-drawing_character but when you use the line drawing characters from Unicode in a text file, you cannot get them as thin as what is shown between lines in your new image.
You are not showing us the content of a file that happens to contain hexadecimal: you are showing us the graphical output of an application that is displaying a hexadecimal dump of something like an operating system or executable.
The difference is very important for this purpose, as it changes everything about the task. If your .raw file does not contain hexadecimal and instead contains binary data then nearly everything we gave you for code is irrelevant. So we need to know the actual file format to proceed.
no i want to read this first given data only 00 12 06 25 32 45 02 35 C2 00 B7 00 FD 00 4C 00 don't want to read first column and last column with special characters. bytes may be empty data also possible.
Please attach a file containing (say) the first 4 lines of data. zip the file to attach it, as .raw files cannot be attached directly.
now i am able to read this hex data in decimal format in other file, but the thing here is first 4 words represents the time tag while reading in decimal format its again converting to decimal(that is already in decimal), i want to read the first words as it is.
now i want to scan my data row wise(in .rdt file format) and count the number of times the pattern is occured in the file( the pattern is like continuous words in a row are like 120 135 78) make a counter value and proceed further for same and counts the counter value for how many times pattern is repeated in .rdt file and scanning should be row wise in .rdt file.
thank you.
? MacroMedia RoboView files? That kind of .rdt ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by