Converting Binary files to ASCII

Hello everyone,
I am not sure how to convert a Binary file into ASCII file by using matlab. I have seen other similar questions about this in this page but I am not sure about the format of my binary file. It contains points from a disparity map of an image. I have also tried using the tutorials from Matlab, but still I get a wrong result.I attach some binary files so that someone can help
This is the matlab tutorial i was talking about:
A = fread(fileID)
A = fread(fileID,sizeA)
A = fread(fileID,sizeA,precision)
A = fread(fileID,sizeA,precision,skip)
A = fread(fileID,sizeA,precision,skip,machinefmt)
[A,count] = fread(___)
Please, any help would be appreciated.
Thanks in advance

3 Comments

" but I am not sure about the format of my binary file"
Without that, not much one can say or do..the creator of the files should be able to tell you what the format is or if it was computed using one of the Matlab functions, that function description should at least provide clues as to how the file could have been written.
[MM Answer moved to comment...dpb]
This is the only thing the creator said:
Additionally we provide the velodyne point clouds for point-cloud-based methods. To save space, all scans have been stored as Nx4 float matrix into a binary file using the following code:
stream = fopen (dst_file.c_str(),"wb");
fwrite(data,sizeof(float),4*num,stream);
fclose(stream);
Here, data contains 4*num values, where the first 3 values correspond to x,y and z, and the last value is the reflectance information. All scans are stored row-aligned, meaning that the first 4 values correspond to the first measurement. Since each scan might potentially have a different number of points, this must be determined from the file size when reading the file, where 1e6 is a good enough upper bound on the number of values:
// allocate 4 MB buffer (only ~130*4*4 KB are needed)
int32_t num = 1000000;
float *data = (float*)malloc(num*sizeof(float));
// pointers
float *px = data+0;
float *py = data+1;
float *pz = data+2;
float *pr = data+3;
// load point cloud
FILE *stream;
stream = fopen (currFilenameBinary.c_str(),"rb");
num = fread(data,sizeof(float),num,stream)/4;
for (int32_t i=0; i<num; i++) {
point_cloud.points.push_back(tPoint(*px,*py,*pz,*pr));
px+=4; py+=4; pz+=4; pr+=4;
}
fclose(stream);
x,y and y are stored in metric (m) Velodyne coordinates.
Well, that's quite a lot of "only"... :)

Sign in to comment.

Answers (1)

OK, that's all you should need...
fid=fopen('yourfile.ext','r');
data=fread(fid,[inf,4],'single');
fid=fclose(fid);
should return you a Nx4 data array of the x,y,z and reflectance values for each of N measurements.

3 Comments

I think that ends up with the data in the wrong order.
data = fread(fid, inf, 'single');
data = reshape(data, 4, []) .';
Yeah, it worked with Walter Roberson's code, but thanks to both of you!
You both were really helpful!
Walter Roberson's code also worked for me. Thank you.

Sign in to comment.

Asked:

on 21 Mar 2019

Commented:

on 1 May 2020

Community Treasure Hunt

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

Start Hunting!