# Assigned variables not working

**URL:** <https://discourse.julialang.org/t/assigned-variables-not-working/88014>\
**Category:** New to Julia\
**Tags:** plotting, error-message\
**Created:** [September 29, 2022, 10:38pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014 "2022-09-29T22:38:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [September 29, 2022, 10:38pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/1 "2022-09-29T22:38:41Z")

</div>

I have multiple plots I am making of a given function that has modifications for each plot, and in each plot method I use the kwarg lw (line weight) so I have 6 plots so for each iteration of the function I have a different line weight variable which I define as lw1, lw2,…,lw6.

Now when I call lw1,…,lw4 in their respective plots, i.e. set lw=lw1 or lw=lw2, it works fine but for lw5 and lw6 I simply get these variables are undefined.

Namely, for lw1,lw2,lw3,lw4 I have something such as

> > plot!(sol\_test4, lw=lw4, layout=(1,3), legend = true,  
> > labels = [nothing nothing nothing],  
> > margin=5mm,  
> > ylabel = [“” “” “”] ,  
> > xlabel = [“” “” “”],  
> > yformatter = :plain,  
> > color = [plot\_col plot\_col plot\_col])

And this causes no issue but

> > plot!(sol\_test5, lw=lw5, layout=(1,3), legend = true,  
> > labels = [nothing nothing nothing],  
> > margin=5mm,  
> > ylabel = [“” “” “”] ,  
> > xlabel = [“” “” “”],  
> > yformatter = :plain,  
> > color = [:gray :gray :gray])

will cause an error. I either get a undefined variable error or a no method found error. If I replace lw=lw5 with something lw=0.8, etc it works fine.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 2:27am UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/2 "2022-09-30T02:27:19Z")

</div>

Can you show us the full error message?

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [September 30, 2022, 5:13pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/3 "2022-09-30T17:13:41Z")

</div>

There’s either this one:

> > > ERROR: LoadError: UndefVarError: lw5 not defined  
> > > Stacktrace:  
> > > [1] top-level scope  
> > > @ ~/Param/theta2.jl:250  
> > > [2] include(fname::String)  
> > > @ Base.MainInclude ./client.jl:451  
> > > [3] top-level scope  
> > > @ none:1

I had lw5 suitably defined, it was defined in the similar fashion as lw1,lw2,lw3 and lw4.

Or this one

> > > Error showing value of type Plots.Plot{Plots.PlotlyBackend}:  
> > > ERROR: MethodError: no method matching Float64(::Num)  
> > > Closest candidates are:  
> > > (::Type{T})(::Real, ::RoundingMode) where T\<:AbstractFloat at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/rounding.jl:200  
> > > (::Type{T})(::T) where T\<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/boot.jl:770  
> > > (::Type{T})(::SymbolicUtils.Symbolic) where T\<:Union{AbstractFloat, Integer, Complex{\<:AbstractFloat}, Complex{\<:Integer}} at ~/.julia/packages/Symbolics/sDAUx/src/Symbolics.jl:139  
> > > …  
> > > Stacktrace:  
> > > [1] convert(#unused#::Type{Float64}, x::Num)  
> > > @ Base ./number.jl:7  
> > > [2] lower(x::Num)  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:61  
> > > [3] show\_json(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, a::Num)  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:314  
> > > [4] show\_pair(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, k::Symbol, v::Num)  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:241  
> > > [5] show\_pair(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, kv::Pair{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:243  
> > > [6] show\_json(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, x::Dict{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:271  
> > > [7] show\_pair(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, k::Symbol, v::Dict{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:241  
> > > [8] show\_pair(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, kv::Pair{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:243  
> > > [9] show\_json(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, x::Dict{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:271  
> > > [10] show\_element(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, x::Dict{Symbol, Any})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:218  
> > > [11] show\_json(io::JSON.Writer.PrettyContext{IOBuffer}, s::JSON.Serializations.StandardSerialization, x::Vector{Dict{Symbol, Any}})  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:293  
> > > [12] #show\_json#3  
> > > @ ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:325 [inlined]  
> > > [13] print(io::IOBuffer, obj::Vector{Dict{Symbol, Any}}, indent::Int64)  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:347  
> > > [14] sprint(::Function, ::Vector{Dict{Symbol, Any}}, ::Vararg{Any}; context::Nothing, sizehint::Int64)  
> > > @ Base ./strings/io.jl:114  
> > > [15] sprint  
> > > @ ./strings/io.jl:108 [inlined]  
> > > [16] json(a::Vector{Dict{Symbol, Any}}, indent::Int64)  
> > > @ JSON.Writer ~/.julia/packages/JSON/NeJ9k/src/Writer.jl:366  
> > > [17] plotly\_series\_json(plt::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Plots ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1044  
> > > [18] js\_body  
> > > @ ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1109 [inlined]  
> > > [19] plotly\_html\_body(plt::Plots.Plot{Plots.PlotlyBackend}, style::Nothing)  
> > > @ Plots ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1097  
> > > [20] plotly\_html\_body  
> > > @ ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1071 [inlined]  
> > > [21] html\_body  
> > > @ ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1049 [inlined]  
> > > [22] embeddable\_html  
> > > @ ~/.julia/packages/Plots/1KWPG/src/backends/web.jl:26 [inlined]  
> > > [23] \_show(io::IOContext{IOBuffer}, #unused#::MIME{Symbol(“text/html”)}, plt::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Plots ~/.julia/packages/Plots/1KWPG/src/backends/plotly.jl:1129  
> > > [24] \_showjuno  
> > > @ ~/.julia/packages/Plots/1KWPG/src/output.jl:270 [inlined]  
> > > [25] showjuno(io::IOContext{IOBuffer}, m::MIME{Symbol(“text/html”)}, plt::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Plots ~/.julia/packages/Plots/1KWPG/src/output.jl:254  
> > > [26] show(io::IOContext{IOBuffer}, m::MIME{Symbol(“application/prs.juno.plotpane+html”)}, plt::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Plots ~/.julia/packages/Plots/1KWPG/src/output.jl:220  
> > > [27] show(io::IOContext{IOBuffer}, m::String, x::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Base.Multimedia ./multimedia.jl:111  
> > > [28] displayinplotpane(x::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Atom ~/.julia/packages/Atom/BEJkS/src/display/showdisplay.jl:51  
> > > [29] display(d::Atom.JunoDisplay, x::Plots.Plot{Plots.PlotlyBackend})  
> > > @ Atom ~/.julia/packages/Atom/BEJkS/src/display/showdisplay.jl:118  
> > > [30] display(x::Any)  
> > > @ Base.Multimedia ./multimedia.jl:328  
> > > [31] print\_response(errio::IO, response::Any, show\_value::Bool, have\_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:0  
> > > [32] (::REPL.var"#45#46"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:277  
> > > [33] with\_repl\_linfo(f::Any, repl::REPL.LineEditREPL)  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:510  
> > > [34] print\_response(repl::REPL.AbstractREPL, response::Any, show\_value::Bool, have\_color::Bool)  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:275  
> > > [35] (::REPL.var"#do\_respond#66"{Bool, Bool, Atom.var"#262#263"{Module}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:846  
> > > [36] #invokelatest#2  
> > > @ ./essentials.jl:716 [inlined]  
> > > [37] invokelatest  
> > > @ ./essentials.jl:714 [inlined]  
> > > [38] run\_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)  
> > > @ REPL.LineEdit /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/LineEdit.jl:2493  
> > > [39] run\_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)  
> > > @ REPL /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/REPL/src/REPL.jl:1232  
> > > [40] (::REPL.var"#49#54"{REPL.LineEditREPL, REPL.REPLBackendRef})()  
> > > @ REPL ./task.jl:423

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 30, 2022, 5:39pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/4 "2022-09-30T17:39:59Z")

</div>

Can you show the code where you define `lw1`, …, `lw6`?

And can you simplify your code? Instead of the plotting function call, just write

```julia
println(lw4) 
println(lw5) 

```

The point is to simplify the code as much as possible to identify exactly where the error is.

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [September 30, 2022, 6:12pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/5 "2022-09-30T18:12:55Z")

</div>

Each one was defined inside of a function in this fashion:

> > p = 5.5  
> > global lw5 =0.6  
> > global plot\_col5 = “gray”  
> > if abs(p-θ[1]) \< 0.5  
> > lw5 = 5.6  
> > plot\_col5 = “black”  
> > end

The only difference for each one is the value of p.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 6:27pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/6 "2022-09-30T18:27:38Z")

</div>

> [@Max\_Beez](#):
>
> [1] top-level scope  
> @ ~/Param/theta2.jl:250  
> [2] include(fname::String)  
> @ Base.MainInclude ./client.jl:451

This looks strange right here. It looks like you are trying to load code into the `Main` module via `include`. Could you tell us more about how you organize and load code? Do you use modules at all? Do you use packages?

My suspicion is that we likely have some kind of scope issue at play here.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 6:35pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/7 "2022-09-30T18:35:02Z")

</div>

Let’s say I had a file blah.jl that had these contents:

```julia
module blah
    global lw5 = 5
end
println(lw5)

```

If I tried to run this, I would get an errror.

```julia
ERROR: LoadError: UndefVarError: x not defined

```

That’s expected. Are you using anything like this?

Do you have any try `try`, `while`, or `for` loops around this piece of code?

You could be running into some confusion with soft global scope. See the following for more details:

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 6:38pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/8 "2022-09-30T18:38:05Z")

</div>

Here is another example that could fail.

```julia
global x = 5
for a in 1:5
    println(x)
    x = 3
end

```

If I try to run that from a file, I get

```julia
┌ Warning: Assignment to `x` in soft scope is ambiguous because a global variable by the same name exists: `x` will be treated as a new local. Disambiguate by using `local x` to suppress this warning or `global x` to assign to the existing global variable.
└ @ ~/src/VersionParsing.jl/blah.jl:4
ERROR: LoadError: UndefVarError: x not defined

```

Which version of Julia are you using? What is the output when you run `versioninfo()` in the REPL?

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [September 30, 2022, 6:39pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/9 "2022-09-30T18:39:24Z")

</div>

I do use packages. I have the following at the top of this particular

> using DifferentialEquations  
> using Plots; plotly(), Plots.PlotlyBackend  
> using Plots.Measures  
> using Distributions  
> using Statistics  
> using ModelingToolkit  
> using LinearAlgebra

I use the Julia language package in Atom, it’s all done in one .jl file, no modules.

> julia\> versioninfo()  
> Julia Version 1.7.2  
> Commit bf53498635 (2022-02-06 15:21 UTC)  
> Platform Info:  
> OS: macOS (arm64-apple-darwin21.2.0)  
> CPU: Apple M1  
> WORD\_SIZE: 64  
> LIBM: libopenlibm  
> LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)  
> Environment:  
> JULIA\_EDITOR = atom -a  
> JULIA\_NUM\_THREADS = 8

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 6:49pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/10 "2022-09-30T18:49:23Z")

</div>

Could you share the entire theta2.jl file? I’m not sure how else to diagnose this further.

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [September 30, 2022, 7:45pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/11 "2022-09-30T19:45:37Z")

</div>

Unfortunately I can’t share the entire file. Thank you for the help, perhaps this topic should be deleted since it probably can’t be rectified and I am unable to share.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 30, 2022, 9:31pm UTC](https://discourse.julialang.org/t/assigned-variables-not-working/88014/12 "2022-09-30T21:31:20Z")

</div>

Do what DNF said then and try to simplify.

Copy the file. Replace all the plotting commands with `println(lw[n])` and boil it down to the minimum example that reproduces the error. Perhaps you will be able to share that?

Also, I wanted to note that development on Julia for Atom has been discontinued in favor of the Julia VS Code plugin.
