metaprogramming


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 = []

Related Links

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);`

Categories

HOME
embedded
phantom-dsl
rsa
numbers
solver
android-json
webix
nstableheaderview
teechart
google-schemas
minecraft-forge
heap
startup
java.util.logging
emoji
comsol
template-engine
oledb
lilypond
springfox
comparator
xunit
strongname
definitelytyped
gpio
appirater
ms-access-2003
wcag
datastax-enterprise-graph
bioinformatics
carrierwave
vex
userdefaults
dotspatial
kodi
fstream
memory-address
rselenium
jwplayer6
numpy-broadcasting
servicestack-text
xdocreport
mcustomscrollbar
android-softkeyboard
softmax
bindingnavigator
photos
xbmc
cnc
podscms
spring-data-couchbase
headless
dt
mouseleave
pybrain
cmp
ampps
branch-prediction
sandcastle
juice-ui
visual-studio-2008-sp1
angular2-testing
dotnetnuke-7
nikeplus-api
synonym
dimension
vorpal.js
project-organization
hadoop-partitioning
libav
atan2
jdi
omnifaces
ms-access-2000
nsurlsessiondatatask
nodelist
angular-gettext
imagefilter
rmysql
mdichild
grails-domain-class
fpdi
maven-scm
light
http-patch
mbunit
gpars
video-codecs
cardreader
cascadingdropdown
wimax
robotlegs
android-loadermanager
wcf-configuration
entity-attribute-value
matlab-load
google-ajax-api
codebase
commerceserver2007
objective-j
dot-emacs
aspmenu
qtabbar
web-application-project
dojo-dnd
sifr3
fdf
unmanagedresources
dsoframer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App