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

**URL:** <https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117>\
**Category:** New to Julia\
**Tags:** question, binaryio\
**Created:** [October 2, 2021, 7:06pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117 "2021-10-02T19:06:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zweta\_Fuze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweta_fuze/32/29615_2.png) [@Zweta\_Fuze](https://discourse.julialang.org/u/Zweta_Fuze)\
**Post date:** [October 2, 2021, 7:06pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/1 "2021-10-02T19:06:36Z")

</div>

% 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

---

<div class="post-metadata">

**Author:** ![GHL](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@GHL](https://discourse.julialang.org/u/GHL)\
**Post date:** [October 2, 2021, 7:33pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/2 "2021-10-02T19:33:51Z")

</div>

You could try:

```julia
#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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 2, 2021, 8:41pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/3 "2021-10-02T20:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zweta\_Fuze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweta_fuze/32/29615_2.png) [@Zweta\_Fuze](https://discourse.julialang.org/u/Zweta_Fuze)\
**Post date:** [October 3, 2021, 9:57am UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/4 "2021-10-03T09:57:00Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Zweta\_Fuze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweta_fuze/32/29615_2.png) [@Zweta\_Fuze](https://discourse.julialang.org/u/Zweta_Fuze)\
**Post date:** [October 3, 2021, 10:11am UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/5 "2021-10-03T10:11:01Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 3, 2021, 10:57am UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/6 "2021-10-03T10:57:03Z")

</div>

> [@GHL](#):
>
> `file.txt`

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 3, 2021, 1:02pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/7 "2021-10-03T13:02:17Z")

</div>

```julia
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

---

<div class="post-metadata">

**Author:** ![Zweta\_Fuze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweta_fuze/32/29615_2.png) [@Zweta\_Fuze](https://discourse.julialang.org/u/Zweta_Fuze)\
**Post date:** [October 3, 2021, 1:49pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/8 "2021-10-03T13:49:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GHL](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@GHL](https://discourse.julialang.org/u/GHL)\
**Post date:** [October 3, 2021, 2:53pm UTC](https://discourse.julialang.org/t/how-do-i-have-to-read-an-unknown-number-of-floats-in-binary-data-till-eof/69117/9 "2021-10-03T14:53:46Z")

</div>

Thanks for sharing your solution, glad we could help 🙂  
Maybe have a look at:  
[Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)  
which explains code formating, etc. to make it easier to help you and for others to also profit from your questions.
