# Passing an array to a function for use as dims in Array{}

**URL:** <https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601>\
**Category:** New to Julia\
**Created:** [September 10, 2019, 12:50pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601 "2019-09-10T12:50:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Mc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_mc/32/10191_2.png) [@David\_Mc](https://discourse.julialang.org/u/David_Mc)\
**Post date:** [September 10, 2019, 12:50pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/1 "2019-09-10T12:50:24Z")

</div>

**Starting a new session solved the issue**  
TIL: If it really seems like it should work, give it a try in a new session.

## 

Original post:  
In:  
[https://discourse.julialang.org/t/read-binary-data-of-arbitrary-dims-and-type/28560](https://discourse.julialang.org/t/read-binary-data-of-arbitrary-dims-and-type/28560)  
I got help on reading binary data (thanks!), but stumbled into a secondary problem.

Using julia 1.1.1 &nbsp;&nbsp; ~~1.0.4~~  
**Please note:** code examples are copied by hand. I can’t have web access and julia one the same machine. Mistakes are possible.

```julia-auto
 function read_dat(filename, dims, T)
  img = Array{T}(undef, (dims)) 
  open(filename) 
  do io 
    read!(io, img) 
  end 
end
```

```julia-auto
#Calling it with this works fine:
data = read_dat("myfilename.img", (251, 260), Float32)
```

```julia-auto
# But trying to use variables for dims does not:
bands=250; lines=260; samples=440;
fn = “myfile.img”;
dtype = Float32;
data = read_dat(fn, (bands,lines,samples), dtype)
```

fn and dtype pass ok, as does just a single variable for dims, but trying to pass all 3 for dims results in  
`no method matching Array{Float32,N} where N(::UndefInitializer, :: Tuple{String,String,String})`  
I don’t understand the error message and didn’t find anything in the docs to decode it.

In use, dims could be 1,2, or 3 values.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 10, 2019, 1:08pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/2 "2019-09-10T13:08:44Z")

</div>

Hm, is this the exact code you’re running? The error message points to your Array constructor (the first line in the function), but just doing this:

```julia
function read_dat(filename, dims, T)
    img = Array{T}(undef, dims) 
end

read_dat("myfilename.img", (251, 260), Float32) # returns 250x260 Array{Float32, 2}

bands=250; lines=260; samples=440;
fn = "myfile.img";
dtype = Float32;
read_dat(fn, (bands,lines,samples), dtype) # returns 250x260x440 Array{Float32, 3}

```

works for me as expected. Which Julia version are you using?

---

<div class="post-metadata">

**Author:** ![David\_Mc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_mc/32/10191_2.png) [@David\_Mc](https://discourse.julialang.org/u/David_Mc)\
**Post date:** [September 10, 2019, 1:39pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/3 "2019-09-10T13:39:07Z")

</div>

hmmm… i typed in your shortened version but still get the same error when trying to use variables for _dims_.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 10, 2019, 1:47pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/4 "2019-09-10T13:47:08Z")

</div>

This is the output from a fresh REPL session in Julia 1.2.0:

```julia
julia> function read_dat(filename, dims, T)
           img = Array{T}(undef, dims)
       end
read_dat (generic function with 1 method)

julia> read_dat("myfilename.img", (251, 260), Float32) 
251×260 Array{Float32,2}:
<snip>

julia> bands=250; lines=260; samples=440;

julia> fn = "myfile.img";

julia> dtype = Float32;

julia> read_dat(fn, (bands,lines,samples), dtype)
250×260×440 Array{Float32,3}:
<snip>

```

Can you provide the full output (minus all the printed empty arrays) from running the above in a fresh session on your machine?

---

<div class="post-metadata">

**Author:** ![David\_Mc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_mc/32/10191_2.png) [@David\_Mc](https://discourse.julialang.org/u/David_Mc)\
**Post date:** [September 10, 2019, 2:00pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/5 "2019-09-10T14:00:48Z")

</div>

Oof! So in a fresh session I get exactly what you get. Everything is ok.  
I don’t understand why a new session would make a difference, but thank you so much.  
I feel a bit silly, but at least I’ve learned something new to try in the future. 🙂

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [September 10, 2019, 2:10pm UTC](https://discourse.julialang.org/t/passing-an-array-to-a-function-for-use-as-dims-in-array/28601/6 "2019-09-10T14:10:27Z")

</div>

This is quite hard to say in hindsight, but there’s alsway a slim chance that by accident you redefined some function by using a funny variable name, so trying in a fresh session often helps - glad it works now!
