Makie on() do block does not see a variable

I am in this situation:

on(topbutton[4].clicks) do click 
    lma,lmaheader,lmafile,tim,xy,tz = load_lma_data(lmafolder,lmafile,1)
    selection[] = lma.qua .&& (0 .< tim .<= 600)
end  

Result: Error in callback: UndefVarError: `lmafile` not defined Stacktrace: [1] (::var"#196#197")(click::Int64) @ Main .....etc.

However, the following just returns the strings without problem:

on(topbutton[4].clicks) do click
    println(lmafolder)
    println(lmafile)
end   

Also, in the REPL I can call without problem:
lma,lmaheader,lmafile,tim,xy,tz = load_lma_data(lmafolder,lmafile,1)

What am I missing?

I assume you have lmafile as a variable in an outer scope. But in this line

lma,lmaheader,lmafile,tim,xy,tz = load_lma_data(lmafolder,lmafile,1)

you assign to it on the left-hand side, which probably causes it to be treated as a local variable in the whole scope, so it doesn’t exist yet when you want to use it as a function argument. At least that’s my guess.

1 Like

You are right, it is the variables on the left-hand side. I then tried this:

    lma2,lmaheader2,lmafolder2,lmafile2,tim2,xy2,tz2 = load_lma_data(lmafolder,lmafile,1)
    # lma = lma2; lmaheader = lmaheader2;
    # lmafolder = lmafolder2; lmafile = lmafile2;
    # tim = tim2; xy = xy2; tz = tz2; 
    selection[] = lma.qua .&& (0 .< tim .<= 600)
end

Like this, it loads the new data but does not display it, as it remains local. I cannot seem to find a simple way to update the global variables with the new data. Enabling the commented lines trigger the same error in the line before, where the function is called. I also tried a form of lma_load_data! without success. What would be the right way to do it?

Update! It works just by using:

global lma,lmaheader,lmafolder,lmafile,tim,xy,tz = load_lma_data(lmafolder,lmafile,-1)