Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read binary file in Julia?

Tags:

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?

like image 459
Jon Avatar asked Jan 30 '20 17:01

Jon


1 Answers

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)
like image 176
David Varela Avatar answered Oct 21 '22 15:10

David Varela