Jon
Jon

Reputation: 103

How to read binary file in Julia?

I have used Matlab and now try to convert some code to Julia.

% Load data in Matlab
fileID = fopen('./data_6000x3199.bin');
Data = fread(fileID,[6000,3199],'single');
fclose(fildID);

However, I have no idea how to read this single type binary file in Julia code. Can someone help this, please?

Upvotes: 10

Views: 6682

Answers (1)

David Sainez
David Sainez

Reputation: 6956

read! will fill an array with data read from a binary file:

julia> x # original array
2×2 Array{Float32,2}:
 1.0  3.0
 2.0  4.0

julia> write("test.bin", x) # write to binary file
16

julia> y = Array{Float32}(undef, 2, 2); # create a container for reading data

julia> read!("test.bin", y) # read data
2×2 Array{Float32,2}:
 1.0  3.0
 2.0  4.0

'single' means single precision floating point, which is represented with Float32 in Julia. Your example MATLAB code will translate to this Julia code:

data = Array{Float32}(undef, 6000, 3199)
read!("data_6000x3199.bin", data)

Upvotes: 15

Related Questions