I have many different variable names or symbols for different data.
I will insert one of those variable at a time in my safe_log( ) function. I expect cbar.label and ax.title to accept that variable name automatically rather than changing variable name manually at every place.
For example i want to insert Ψ variable name or symbol in my plot’s Title and color bar.
Functions cannot know about the names of the variables passed to them. You either need to write a macro that then passes both name and variable to the function as two separate arguments, or use a dictionary to hold your variables, then you can pass a string or symbol directly and retrieve the data this way.
function m(s::Symbol, value)
println("The symbol is $s and the value is $value")
end
macro m(variable::Symbol)
:(m($(QuoteNode(variable)), $(esc(variable))))
end
julia> x = 1
1
julia> @m(x)
The symbol is x and the value is 1
Sorry, You misunderstood. I have a variable Ψ and i will insert its data in safe_log( ) function. After that i will insert that variable Ψ in cbar.label and ax.title to plot symbol Ψ in title and color bar.
safe_log(x::Float64) = x > 0 ? log(x) : 1.0
# Insert variable that you want to plot in z_log function.
z_log = safe_log.(Ψ)
You should provide a proper MWE - in your first post you aren’t actually creating a plot at all.
Both your initial post and your reply above seem to suggest that what you’re trying to do is exactly what @jules describes, so if you’re actually trying to do something else you need to be more explicit about what that is.
Please can you give dictionary way example. Thanks.
I got error while using @m( ) inside another function.
ERROR: LoadError: UndefVarError: value not defined in Main
Suggestion: check for spelling errors or missing imports.
Stacktrace:
[1] main(directory::String, N_r::Int64, N_θ::Int64, r::Vector{Float64}, θ::Vector{Float64}, μ::Float64, dms::Int64)
@ Main ~/Videos/cos_veloclect.jl:95
[2] macro expansion
If i define m function and macro inside my function then i get local error.
julia> using GLMakie
julia> data = Dict("ψ" => rand(10))
Dict{String, Vector{Float64}} with 1 entry:
"ψ" => [0.687504, 0.0257956, 0.282592, 0.390958, 0.285716, 0.466667, 0.817611, 0.601683, 0.142801, 0.138936]
julia> function plot_a_variable(varlabel, data_dict)
lines(data_dict[varlabel], axis = (; title = "A plot of $varlabel"))
end
plot_a_variable (generic function with 1 method)
julia> plot_a_variable("ψ", data)
Please see this MWE. My data is available at that earlier post. What modifications should i do in my code so that i have to change variable(here Ψ) only at single place.
using GLMakie, Meshes
x = include("x.jl") # Matrix of x-coordinates
z = include("z.jl") # Matrix of z-coordinates
Ψ = include("psi.jl") # Data matrix
grd = StructuredGrid(x, z)
function m(s::Symbol, value)
println("The symbol is $s ")
end
macro m(variable::Symbol)
:(m($(QuoteNode(variable)), $(esc(variable))))
end
@m(Ψ)
safe_log(x::Float64) = x > 0 ? log(x) : 1.0
z_log = safe_log.(Ψ)
fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)
ax.title = "Ψ and Contour Plot (data.$file)"
cbar = Colorbar(fig[1, 2])
cbar.label = L"log(Ψ)"
# Save the plot
save("Demo_Meshes.png", fig)
display(fig)
You were meant to transfer the macro technique I showed you to your own function if you just call @m nothing happens to your own code, it was an example