Problems using @png inside of a loop in Luxor

Hi, I´m quite new to Julia and I´ve been stuck on a problem for quite a while now. I´m currently working on a Jupyter file and with Julia version 1.5.3 on a Windows computer, using Luxor.
I have an array of arrays and a function (dibujar_matriz) which creates a table based on the entries of each array.

function dibujar_matriz(M)
background(“black”)
t = Table(size(M,1), size(M,1), 500/size(M,1), 500/size(M,1))
for i ∈ 1:size(M,1)
for j ∈ 1:size(M,1)
if M[j,i] == 1
sethue(“white”)
box(t, j, i, :fill)
end
end
end
end

@png dibujar_matriz(M) (where M is a previously defined matrix) works fine, but I wish to display and save directly in my computer (not in Jupyter) each element of my array. Yet

for i ∈ 1:length(array)
@png dibujar_matriz(array[i])
end

fails to do so. It only saves the last element of my array in Jupyter and with an arbitrary name. I would like to display each of the elements of my array, and not only the last one, and save each png file with a name of the sort “Matrix [ i ]” where [i] changes with each iteration.
Any help would be truly appreciated.

I’d recommend you using explicit file naming rather than relying on the automatic time stamps. Perhaps:

let
    M = rand(1:10, 3, 3)
    counter = 1
    for i ∈ 1:length(M)
        @png begin
            text(string(i))
        end 400 400 "$(counter).png"
        counter += 1
    end
end

is more the kind of thing.

It looks like the macro is expanding the timestamp once and then you’re just repeating that timestamp for every iteration… I’ll be sure to reprimand the person responsible :rofl:

1 Like

I think that works! Yet I made a terrible mistake which is causing me tons of trouble. I ran your code but I changed the name of your array to “H”, but forgot to change to length(H) in the for loop, so instead “i” varied over length(M), where M was a 100x100 matrix which I had previously defined. I now have 10,000 .png files which are messing up with my computer and programs. :sob:

Is there a way to delete them from the Julia or from another Jupyter notebook? I have tried to delete them as I would delete any other Jupyter notebook but it slowed down my computer so much that I can’t do it.

Sorry to hear that… I don’t want to give you advice about deleting files, though, especially on Windows, which I don’t know. Julia has a rm() function - but isn’t there also a Windows shell?

I’ve done this before myself - I usually remember to create a folder first these days…:grinning:

Thank you very much! I’ll see what I can do with those files. On the other hand your answer has been the solution to my problem. Thanks!