# Binary\_reading

**URL:** <https://discourse.julialang.org/t/binary-reading/25207>\
**Category:** New to Julia\
**Tags:** binaryio\
**Created:** [June 12, 2019, 7:52pm UTC](https://discourse.julialang.org/t/binary-reading/25207 "2019-06-12T19:52:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [June 12, 2019, 7:52pm UTC](https://discourse.julialang.org/t/binary-reading/25207/1 "2019-06-12T19:52:12Z")

</div>

Hello!  
I need help with the function for reading the binary file. I’m using

```julia
function read_bin(filename, dims::N) where N<:Integer
    stream = open(filename, "r");
    data = read(stream, Float16, dims);
    close(stream);
    return data
end

```

but this is giving error that I can not solve. Can someone help me?

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [June 12, 2019, 8:20pm UTC](https://discourse.julialang.org/t/binary-reading/25207/2 "2019-06-12T20:20:16Z")

</div>

There are couple options, I think the most straight forward one is this:

```julia
function read_bin(filename, dims::N) where N<:Integer
    # initial an array with known size
    data = Vector{Float16}(undef,dims)
    # or data = zeros(Float16,10)
    stream = open(filename, "r");
    # read stream into the pre-allocated typed-vector
    read!(stream, data);
    close(stream);
    return data
end

```

It looks like you are new to Julia, Welcome, you can ask this is kinda general question in First Step [https://discourse.julialang.org/c/user/first-steps](https://discourse.julialang.org/c/user/first-steps). You will get lots of help from there.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 12, 2019, 8:42pm UTC](https://discourse.julialang.org/t/binary-reading/25207/3 "2019-06-12T20:42:07Z")

</div>

Even more compactly:

```julia
read_bin(filename, dims) = read!(filename, Vector{Float16}(undef, dims))

```

Argument-type declarations aren’t necessary, but if you want them for clarity I would suggest `read_bin(filename::AbstractString, dims::Integer)` (no `where` clause` is needed).

Note that the above code is endian-dependent; you could use `ntoh` or similar on the result to convert endianness if needed, or use a [portable file format like HDF5](https://github.com/JuliaIO/HDF5.jl).

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [June 27, 2019, 1:28pm UTC](https://discourse.julialang.org/t/binary-reading/25207/4 "2019-06-27T13:28:37Z")

</div>

Thanks!.  
Yes, i’m new in julia.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [June 27, 2019, 1:43pm UTC](https://discourse.julialang.org/t/binary-reading/25207/5 "2019-06-27T13:43:23Z")

</div>

I have another problem. I’m trying use the function ecdf for an array 2-D, but is giving this error:  
" MethodError: no method matching ecdf(::Array{Float16,2})  
Closest candidates are:  
ecdf(!Matched::AbstractArray{T\<:Real,1}) where T\<:Real at /home/user/.julia/packages/StatsBase/rTkaz/src/empirical.jl:50"

This is a line that gives error, where matR is 2D array.  
“mX = reshape(ecdf(matR)(matR), (lin, col))”

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 27, 2019, 1:58pm UTC](https://discourse.julialang.org/t/binary-reading/25207/6 "2019-06-27T13:58:06Z")

</div>

`ecdf` doesn’t work on arrays of rank \> 1, presumably because it doesn’t want to assume which ordering you intend. One way to flatten the array so that `ecdf` accepts it is

```julia
ecdf(vec(A))

```

for some array `A`. Note that this will give you column-wise (first index first) ordering because this is Julia’s memory layout. You can also use `reshape`. Note also that you can get the documentation for functions in the Julia REPL by entering `?functionname`.

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [June 27, 2019, 2:06pm UTC](https://discourse.julialang.org/t/binary-reading/25207/7 "2019-06-27T14:06:45Z")

</div>

Thanks!  
This is a normal array of data read with line and col. Still is does not work?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 27, 2019, 2:09pm UTC](https://discourse.julialang.org/t/binary-reading/25207/8 "2019-06-27T14:09:29Z")

</div>

You’d need to provide some information about the object you are trying to flatten and what errors you are getting for it to be possible to help you. Please see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [June 27, 2019, 2:14pm UTC](https://discourse.julialang.org/t/binary-reading/25207/9 "2019-06-27T14:14:31Z")

</div>

Yes, thanks. I will read.
