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 is the correct syntax in metaprogramming assignment in Julia?
How can I call .WHY on a subroutine in a class in Perl 6?
Using Roslyn, if I have an IdentifierNameSyntax, can I find the member type it refers to (field, property, method…)
Elixir - Define functions in module by metaprogramming
Disambiguate the various quoting mechanisms in Julia metaprogramming
How to initialize list using macros and then generate same set of functions in 2 different modules?
Type-safe metaprogramming? Using strings to query ceylon.language for meta information?
Interpolating an Expression into an Expression
Loop unrolling? in Julia with metaprogramming
Macro for a function template: escaping issues
Macro that defines macro returning struct constant
Array of Expressions to an Array Definition
Code generation from restricted set of input
System of equations using metaprogramming
How to define a partial copy constructor metaprogramically in Julia?
How to interpolate into a Julia “for” expression?