# Save variables to .mat file or .jld2 file by metaprogramming

**URL:** <https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 11, 2020, 5:13pm UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471 "2020-09-11T17:13:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Logic\_Logical](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logic_logical/32/14809_2.png) [@Logic\_Logical](https://discourse.julialang.org/u/Logic_Logical)\
**Post date:** [September 11, 2020, 5:13pm UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471/1 "2020-09-11T17:13:36Z")

</div>

Here is a normal code to save variables to mat file (no variable name modification)

```julia
using FileIO, MAT
a = 1
b = 2
c = 3
matwrite("savemat.mat", Dict("a" => a,"b" => b, "c" => c))

```

The question is: How can I write a function or a macro to like this.

```julia
savetomatfile("savemat.mat",a,b,c)

```

I know only basics of metaprogramming.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 11, 2020, 5:49pm UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471/2 "2020-09-11T17:49:40Z")

</div>

This is a really good use-case for a macro. In fact, `JLD2` already has `@save` which does basically what you are looking for.

If you want to learn how to implement this yourself, I would suggest starting with a simpler macro that takes a set of variable names and produces the dictionary mapping those names as strings to their current values.

This might help you start off:

```julia
julia> macro map_name_to_value(variable::Symbol)
         quote
           $(string(variable)) => $(esc(variable))
         end
       end
@map_name_to_value (macro with 1 method)

julia> a = 1
1

julia> @map_name_to_value a
"a" => 1

```

That macro just takes a single variable and produces `"variable name" => value`. You could extend that to take multiple variables in and then construct a dict out of those `name => value` pairs.

Finally, you could change your macro to call “matwrite” with that dict.

---

<div class="post-metadata">

**Author:** ![Logic\_Logical](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logic_logical/32/14809_2.png) [@Logic\_Logical](https://discourse.julialang.org/u/Logic_Logical)\
**Post date:** [September 12, 2020, 1:25am UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471/3 "2020-09-12T01:25:48Z")

</div>

Thanks,  
I’m stuck with how to get the value inside loop in the macro

```julia
a = 1
b = 2
c = 3
macro map_name_to_value2(vars::Symbol...)
   quote
       for var in $(esc(vars))
           println(var)
       end
   end
end

```

```julia
julia> @map_name_to_value2(a,b,c)
a
b
c

```

Can you tell me how to get the values (i.e, 1,2,3)?

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [September 12, 2020, 5:35am UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471/4 "2020-09-12T05:35:21Z")

</div>

Hi,  
the trick is to interpolate with $.

If you want to have a look at something more advanced that does something similar to what you want, have a look at the `@save` macro of JLD2 defined in `src/loadsave.jl`

---

<div class="post-metadata">

**Author:** ![Logic\_Logical](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logic_logical/32/14809_2.png) [@Logic\_Logical](https://discourse.julialang.org/u/Logic_Logical)\
**Post date:** [September 13, 2020, 1:24am UTC](https://discourse.julialang.org/t/save-variables-to-mat-file-or-jld2-file-by-metaprogramming/46471/5 "2020-09-13T01:24:23Z")

</div>

I changed from

```julia
println(var)

```

to

```julia
println(eval(var))

```

Then I get the values 1,2,3
