如何在Julia中读取二进制文件?


9

我使用过Matlab,现在尝试将一些代码转换为Julia。

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

但是,我不知道如何在Julia代码中读取此单一类型的二进制文件。有人可以帮忙吗?

Answers:


9

read! 将用从二进制文件读取的数据填充数组:

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'表示单精度浮点,用Float32Julia 表示。您的示例MATLAB代码将转换为以下Julia代码:

data = Array{Float32}(undef, 6000, 3199)
read!("data_6000x3199.bin", data)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.