I need to create many variables within a function and input these data into numerous functions which conduct operations on the data. How can I do this without declaring the variables as globals? As I understand it variables defined within a function are not accessible outside that function unless they are defined as globals. I have 50+ variables which vary in dimensions (i.e., I have scalars, vectors, and matrices).
Here’s a toy example of this issue. I do not know how to access the three variables defined within prepare_data() in the functions that follow it.
function prepare_data()
# below are two examples of variables constructed. The actual function creates 50 or so variables which vary in dimension
data = readdlm("data.csv", ',', skipstart=1, Float64)
var1 = data[:, 1]
var1_index = findall(var1.==1)
var1_dummy = (var1.==var1')
end
function dosomething1()
# construct t as a function of the data in prepare_data() and return
t = var1[var1_index]
return t
end
function dosomething2()
# use data from prepare_data() again
t = dosomething1() - var1[var1_index]
return t
end
I guess a workaround would be to store (without the function declaration) the code that creates the variables in a different file (say prepare_data.jl) and include it later in the main file.
call it in the master file as include("prepare_data.jl"). I can then go about my calculations since the variables defined in prepare_data.jl are accessible in the rest of the document.
probably just stick them all in a named tuple.
and return that from the function.
which can be done by writing (; a, b, c)
and then accessed as x.a etc after you assign that return to x.
But also consider restructuring your code to not need to do this.
Its rare that real functions needs to deal with more than a handful of variables.
Once appropriately scoped into nice distinct functions with clear purpose.
I am not sure how these two compare to const globals, but surely the NamedTuple will have better performance than Dicts unless you need to frequently change the values in the NamedTuple fields.
If these variables are going to be consistent over time, then you may even consider creating an immutable struct type rather than a NamedTuple. You can just pass this struct around.
Rather than having one big struct, you could create smaller structs, and then build the larger struct out of those structs. Then you might not need to pass the entire struct. You could just pass the substruct around.
Thanks for all the replies. I ran into a memory issue and I am not sure if it is due to the use of const. The OS automatically kills the session about 4 hours in citing memory issues. Would the use of NamedTuple, struct, or dictionaries reduce the memory used?