Assigned variables not working

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.

Can you show us the full error message?

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

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

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

println(lw4) 
println(lw5) 

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

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.

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.

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

module blah
    global lw5 = 5
end
println(lw5)

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

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/

Here is another example that could fail.

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

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

┌ 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?

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

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

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.

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.