Save variables to file at runtime
I'm looking for some functionality in Julia comparable to Matlab's save('myfile.mat', 'myvar1', 'myvar2') For example, using HDF5.jl, it is easy to do #write filename myvar1 myvar2 But this requires that I know exactly which variables I want to write to the file. I'd like to be able to choose which variables to write at run time, in a function scope, and specify their names using symbols or strings. vars = [:myvar1, :myvar2] #write filename vars What would be the best way to accomplish this? EDIT I know that I can use save from JLD.jl as save("file.jld", "myvar1", myvar1). But I want to be able to save a list of variables that are not known at compile time, allowing a single call to save (or similar): if condition myvar1 = 1 vars = [:myvar1] else myvar1 = 1 myvar2 = 2 vars = [:myvar1, :myvar2] end # what goes here? save(filename, vars...)
You may want to take a look at the JLD package, which builds on HDF5 with better support of user-defined Julia types. Both HDF5 and JLD provide functions for save that take run-time names for variables.
You can use serialize and deserialize: vars = Dict() if condition myvar = 1 vars[:myvar1] = myvar1 else myvar1 = 1 myvar2 = 2 vars[:myvar1] = myvar1 vars[:myvar2] = myvar2 end f = open( filename, "w" ) serialize( f, vars ) close( f ) to read: f = open( filename, "r" ) vars = deserialize( f ) close( f ) if you don't need to save variable names you can use array instead of dict: vars = 
What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?
Multiple #:build macros
Julia automatically generate functions and export them
Dealing with end keyword in macros for array indices
Implementation of AnyMap and runtime overhead of `struct Port(u32);`