How do I have to read an unknown number of floats in binary data till EOF?

% MATLAB for low-level reading of 32bit-floating point numbers
f=fopen(‘Float32.dat’,‘r’);
A=fread(f,[100 inf],‘single’); % important for me to use inf
fclose(f);

Julia

f=open(“Float32.dat”,“r”);
A= read(???);
close(f);

Question: What Julia-code do I have to insert for the question marks?

I’m new to Julia coming from Matlab and tired of license gymnastics. I found out, that there are a couple of features in Julia that are better than in Matlab (you don’t have to repmat a vector to add it to a matrix) but I even get stuck in elementary stuff. I’ve tried to search for an answer for the above question but I didn’t find anything, not even in the pdf-manual from Julia. Please tell me, if there is anything I haven’t considered. There are even packages for reading MAT-Files but there is no example for this simple elementary low-level reading of Float32-numbers shown above (instead string, CSV and UInt8). To my feeling there must exist an easy one-liner solution in Julia. I would be happy to have an example.
Wishing you a nice evening
Zweta Fuze

You could try:

#Testdata
input = rand(Float32, 10)
open("file.dat", "w") do io
       write(io, input)
end
#Read in again

output = Vector{Float32}(undef, filesize("file.dat")÷4)
read!("file.dat", output)

This assumes only Float32 as file content.
*Replaced txt extension

2 Likes

can you provdie a .dat file? are they string or just raw bytes?

@GHL
Thank you so much. That is what I needed. Incredible, that there is no example of this one in the documentation but tons of (U)Int8, CSV, strings and textfiles. Now I found out, it even works with matrices like
A=Array{Float32}(undef,100,filesize(“Float32_100xN.dat”)÷sizeof(Float32)÷100);
read!(“5kHz.f32”,A)
I need this for reading multichanneldata, that are sampled with 24bit but stored in floats of 32bit. That’s what it does. Sure it would have been more comfortable being able to use [100 inf] like in Matlab, but I can live with this solution. Thank you very much.

1 Like

@jling
raw Float32
These are data of a multichanneldevice. Data could be produced by
write(“Float32_100xN.dat”,randn(Float32,100,10000))
In Matlab this could be read with
f=fopen(‘Float32_100xN.dat’,‘r’);
A=fread(f,[100 inf],‘single’);
fclose(f);
I learnt up to now that in Julia obviously you have to replace inf by 10_000 in this case. Thank you for your interest.

As the output file is binary, giving it a *.txt extension may be misleading

1 Like
julia> write("Float32_100xN.dat",randn(Float32,100,10000));

julia> reshape(reinterpret(Float32, read("Float32_100xN.dat")), 100, :)
100×10000 reshape(reinterpret(Float32, ::Vector{UInt8}), 100, 10000) with eltype Float32:

so you just need reinterpret then

In my case it worked even without reinterpret

generate Matrix 100x10000 with random Float32

A=randn(Float32,100,10000);

store it

write(“Float32_100xN.dat”,A)

generate Matrix B with equal size as A

B=Array{Float32}(undef,100,filesize(“Float32_100xN.dat”)÷100÷sizeof(Float32));

read contents of Float32_100xN.dat into B

read!(“Float32_100xN.dat”,B)

compare it

isequal(A,B)
turned out TRUE

My configuration is the following
Julia Version 1.4.1
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: Intel(R) Core™ i3-4130 CPU @ 3.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-8.0.1 (ORCJIT, haswell)
Thank you for your interest

Thanks for sharing your solution, glad we could help :slight_smile:
Maybe have a look at:
Please read: make it easier to help you
which explains code formating, etc. to make it easier to help you and for others to also profit from your questions.