Matrix of type textbox?

Hey, I want to store my instances of Makie.Textbox in a matrix of size m=(L,L) where L is an integer.
The ideal situation would be:

matrix = zeros(m, Makie.Textbox)
for ...
  #assign different parts of the matrix to different textboxes
end

Unfortunately, that zeros function doesn’t work. It feels weird to make a custom zeros function that creates a new figure, and makes each “zero” textbox be inside of that figure. Is there a better way to do this?
Thank you!

zeros doesn’t make sense here because the array is not numeric. Just do

matrix = Array{Makie.Textbox}(undef, m)

to allocate an uninitialized 2d array.

1 Like

You can check isundefined(array, i, j) to check if that needs to be initialized as well, iirc

I think you mean isassigned?

Usually, rather than preallocating an array with the right return type and filling that, I find it easier to use map which is like a for loop with automated storage. If you need a matrix as output, you’d need some 2d iterator as input, too, as for ..., for ... syntax doesn’t work with map. Iterators.product can fulfill this purpose, like. Here’s an example with strings, but those could also return Textboxes:

julia> map(Iterators.product(1:3, 1:3)) do (i, j)
           "textbox $i, $j"
       end
3×3 Matrix{String}:
 "textbox 1, 1"  "textbox 1, 2"  "textbox 1, 3"
 "textbox 2, 1"  "textbox 2, 2"  "textbox 2, 3"
 "textbox 3, 1"  "textbox 3, 2"  "textbox 3, 3"

A comprehension is also pretty convenient:

julia> ["textbox $i, $j" for i=1:3, j=1:3]
3×3 Matrix{String}:
 "textbox 1, 1"  "textbox 1, 2"  "textbox 1, 3"
 "textbox 2, 1"  "textbox 2, 2"  "textbox 2, 3"
 "textbox 3, 1"  "textbox 3, 2"  "textbox 3, 3"
1 Like

Ah my bad, it’s actually isdefined(array, i, j).