I want to get binary data from a bin file.

11 views (last 30 days)
薫
on 6 Feb 2025
Commented: on 9 Mar 2025
How can I get the binary data from the bin file, obtained by Labview?
In Labview, we obtained image data (bin file and bmp file) of 256x256pixel atoms.
Attached below is the atomic image of the bmp file obtained by Labview.
I would like to retrieve the binary data of a 256x256 pixel atomic image from a bin file in matlab and convert the binary data into an image.
By doing so, I would like to confirm that the atomic image in the above bmp file and the atomic image drawn from the acquired binary data are the same.
  1 Comment
Walter Roberson
Walter Roberson on 6 Feb 2025
However, it appears that LabView .bin files have arbitrary binary formats, and do not typically have headers indicating the data format. Perhaps you might get lucky and there will be a header indicating the array sizes.

Sign in to comment.

Answers (2)

Umar
Umar on 7 Feb 2025

Hi @大地 ,

After going through your comments, in order to retrieve binary data from a LabVIEW-generated bin file in MATLAB and convert it into an image, you can follow these steps:

1. Read the Binary File: Use the fopen and fread functions to read the binary data from the bin file.

2. Reshape the Data: Since the image is 256x256 pixels, reshape the data into a 2D matrix.

3. Convert to Image: Use the imshow function to display the image.

Here is a sample code snippet to achieve this:

% Specify the path to the bin file
filePath = 'path_to_your_file.bin';
% Open the binary file
fileID = fopen(filePath, 'r');
% Read the binary data
data = fread(fileID, [256, 256], 'uint8'); % Adjust data type as necessary
% Close the file
fclose(fileID);
% Display the image
imshow(data, []);
title('Image from Binary Data');
% Optionally, read and display the BMP file for comparison
bmpImage = imread('path_to_your_file.bmp');
figure;
imshow(bmpImage);
title('Original BMP Image');

This code will help you visualize the binary data as an image and allow you to compare it with the BMP file to confirm their similarity. Ensure that the data type in fread matches the format used in LabVIEW.

Hope this helps.

  11 Comments
Umar
Umar on 9 Feb 2025

Hi @大地,

Please see attached. I did comparison check and both images are not equal. I did find out that there is array size are not compatible and images are different dimensions.

Umar
Umar on 9 Feb 2025

Also, please see attached code.

Sign in to comment.


Walter Roberson
Walter Roberson on 9 Feb 2025
Edited: Walter Roberson on 9 Feb 2025
I was right to be concerned about the file format.
The actual file format involves an 8 byte header that gives array sizes. Then the data is in double precision. Data values range from 0 to 2.77
filenames = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1825621/20250120%20111341_xyfreq450_xamp30_yamp30.zip');
[fid, msg] = fopen(filenames{1});
if fid < 0; error('cannot open file "%s" because "%s"', filenames{1}, msg); end
size1 = fread(fid, 1, '*uint32', 'ieee-be');
size2 = fread(fid, 1, '*uint32', 'ieee-be');
data = fread(fid, [size1 size2], '*double', 'ieee-be');
fclose(fid);
pcolor(data, 'edgecolor', 'none');
colormap(copper)
  8 Comments
Walter Roberson
Walter Roberson on 12 Feb 2025
The image is created in double precision, so 53 bits of precision.
That 53 bits of precision is being mapped through your color table. The size of color tables typically defaults to 256 entries, but you could, for example,
colormap(copper(65536))
to use 65536 color table entries.
The data itself appears to be numbers such as 2.14, with a range from 0 to 2.77 . All of the values appear to be rounded to the nearest 1/100, so in theory there are 278 of them (0 to 277, divided by 100). log2(278) is marginally over 8. So the data source is theoretically 9 bits
薫
on 9 Mar 2025
Thank you very much for your interest.
I created a program based on what you said and it worked.
Thank you very much.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!