Problem with variable scope when translating from Matlab to Julia

Hi guys my problem is as follows:

buff = g0
for i_exh in 1:k
    f_exh = obj_rbfS3(x + y_exh[i_exh] * v, X, ...)
    if f_exh[1,1] > buff[1,1]
        if i_exh > 1
            gp_exh = rbf_dirS3(x + y_exh[i_exh - 1] * v, X, ...)
            if gp_exh[1,1] > 0
                i_exh = i_exh - 1
            end
        end
        @show i_exh
        break
    end
    buff = f_exh
end

# Step S4 (S3 in paper)
if i_exh > 1
     Xq[ii, :] = transpose(x + (y_exh[i_exh - 1] + y_exh[i_exh])/(2 * v))
else
    Xq[ii, :] = transpose(x + y_exh[i_exh] * v)
end

If I use this code which reflects directly the Matlab code I get the following error:

ERROR: LoadError: UndefVarError: i_exh hot defined

and I understand that when when I do:

# Step S4 (S3 in paper)
if i_exh > 1
     Xq[ii, :] = ....

the i_exh is out of its local scope.

My question is: Is there any way to fix this situation so the code could work as it does in Matlab version?

I didn’t find any Domain that would have fit better my question, and I am sorry if I posted it in a wrong Domain.

Thank you very much in advance for your support.

Cheers.

Ergnoor

1 Like

Hey! Have you read this page? Scope of Variables · The Julia Language
It’s usually a good idea to put the code in a function rather than operating in global scope. If you want to modify the loop variable, are you sure you want to use a for loop? You may use the keyword outer to have the loop variable refer to an outer variable, in which case you must initialize it before the loop.

1 Like

In case you insist on running this in a script (not in a function), then this pattern works

for i = 1:10
  global i2 = i
end
println(i2)

There is! For now, use jupyter as your interface for “scripting” as you would the matlab ide. Then when you want to define .jl files and use Atom, put anything with loops in a function instead of a top-level script. If you do this, you will never run into this problem again.

You keep suggesting this in all scope-related threads, but it is not always the solution. In this case for example, the loop variable itself (i_exh) is never available outside of the loop and that has nothing to do with “softscope” as enabled in IJulia, Julia REPL 1.5.

2 Likes

Others told a matlab user to read up on the complicated scoping rules. For beginners coming from matlab they are much better off using jupyter, as it is far more intuitive. I didn’t want a new user to be scared off. Global variables are very different in matlab.

That said, for this it looks like you are 100 percent right that it wouldn’t fix it immediately, but it is wrong to say that Jupyter is orthogonal, as it makes the solution easier for this type of user.

For a matlab user on jupyter, the best solution is to create a variable outside of the loop, then modify it in the loop, and it works exactly as they would expect (and without a global keyword) . So, @Ujku_KU my suggestion is to look at this code

val = 0
for i = 1:10
    val = i
    if i > 5
        break
    end
end
println(val)

Which works great in Jupyter. Also not that if I put it in a function

function f()
    val = 0
    for i = 1:10
        val = i
        if i > 5
            break
        end
    end
    println(val)
end
f()

It works great at well. You would want to make sure it was in a function when using Atom or putting it in a .jl file.

So to summarize, and correct my original statement given what @fredrikekre pointed out.

A key difference between matlab and Julia is that loops have their own scope, so any variables created in them (including the indices) are not available outside of the scope. This has nothing to do with global variables,and I think you will find that it is a much better approach than matlab.

So you need to have the stuff in the loop modify something that was available outside of it. If you use jupyter, you will not need to think about the difference between doing that within a function and within a script. It will be intuitive

1 Like

If you want to learn to use a new language, isn’t learning the scoping rules a rather good idea? I think it’s a mistake to think that one can just switch to a new language without having to learn anything new. Reading the julia manual is a rather small investment in time that pays off quite soon. Also, using jupyter is a very different experience to using an IDE and it might just as well cause additional headache alongside patching up some scoping issue.

2 Likes

Hi guys and thank you very much all of you for your support, I appreciate it very much.

Cheers.

Ergnoor

3 Likes