How to read a .dat file, containing binary data?

The file is very large and ve to read by plotting those binary data without changing it to decimal format.

 Accepted Answer

Ok. You need to open the file, for binary read access. Do that like this
fid = fopen('D:\myfile.dat','r');
Then, you need to read out what is in the file. You know the file format. You can only read out a number of bytes at a time. To read out one byte, and treat it like an unsigned 8 bit integer:
number = fread(fid,1,'*uint8');
To read out all the bytes as unsigned 8 bit integers:
numbers = fread(fid,inf,'*uint8');
That number(s) will print to screen in the range of 0 to 255. If you want to see the binary sequence that actually exists in RAM:
dec2bin(number)
To close the file:
fclose all;

3 Comments

Sir, As, per your suggestion ve tried n I got the output as:
>> Untitled >> Untitled >> Untitled >> Untitled >>
as I ve saved as untitled.m
But if I ve to make a plot then what should I do?
Well, for example:
fid = fopen('D:\myfile.dat','r'); % remember to change the filename to the file you actually want to read
tenthousand8bitunsignednumbers = fread(fid,10000,'*uint8');
fclose(fid);
plot(tenthousand8bitunsignednumbers)
Thanku Sir, its working....!!!
Thanks & Regards

Sign in to comment.

More Answers (2)

fid= fopen('your_file.dat');
data=fread(fid);
fclose(fid);

3 Comments

But this is not working...!!!
Not working how? is there any error message? can you post your code?
Sir, the error msg m getting is as follows
Error using fread Out of memory. Type HELP MEMORY for your options.
Error in Untitled (line 2) data=fread(fid);

Sign in to comment.

close all; clear all; clc;
fx0 = fopen('sortedOFET.txt','r'); x0 = fscanf(fx0,'%f%f%f'); Ioff=fx0(:,1); Ion=fx0(:,2); gm=fx0(:,3); [a b]=size(fx0);
% % P3HT+CuTPP_TNT(75) j=2; k=1; for i=1:2:13 j=i+1; BEoff(k)=Ioff(i); AEoff(k)=Ioff(j); diffoff(k)=AEoff(k)-BEoff(k); peroff(k)=(diffoff(k)/BEoff(k))*100;
BEon(k)=Ion(i);
AEon(k)=Ion(j);
diffon(k)=AEon(k)-BEon(k);
peron(k)=(diffon(k)/BEon(k))*100;
ratioBE(k)=BEon(k)./BEoff(k);
ratioAE(k)=AEon(k)./AEoff(k);
perratio(k)=((ratioAE(k)-ratioBE(k))./ratioBE(k))*100;
BEgm(k)=gm(i);
AEgm(k)=gm(j);
diffgm(k)=AEgm(k)-BEgm(k);
pergm(k)=(diffgm(k)/BEgm(k))*100;
j=j+2;
k=k+1;
end
peroff=peroff(:);
peron=peron(:);
pergm=pergm(:);
perratio=perratio(:);
diffoff=diffoff(:);
diffon=diffon(:);
diffgm=diffgm(:);
data=[peroff peron pergm perratio diffoff diffon diffgm];
sir,this is my code and when i am trying to open it ,i am not able to execute it and i am getting error as follows what should i do?.
??? Attempt to execute SCRIPT on_off_gm_ratio_sortedOFET as a function:
D:\matlab codes\on_off_gm_ratio_sortedOFET.m
7.22E-09 2.99E-06 -1.99E-08
8.08E-08 3.49E-06 -1.77E-08
3.25E-09 1.99E-06 -9.30E-09
4.71E-08 2.40E-06 -1.92E-08
2.94E-08 2.81E-06 -1.31E-08
7.21E-08 2.39E-06 -1.80E-08
3.29E-08 2.39E-06 -1.11E-08
3.29E-08 2.39E-06 -1.20E-07
7.03E-08 5.21E-06 -1.86E-08
1.14E-07 5.30E-06 -7.40E-08
7.92E-08 4.71E-06 -2.05E-08
8.39E-08 4.82E-06 -1.50E-08

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 8 Aug 2014

Commented:

on 7 Jan 2016

Community Treasure Hunt

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

Start Hunting!