# Is it possible for the MAT package to export multiple data sets using a single call of the \`write\` command?

**URL:** <https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 14, 2018, 2:19pm UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716 "2018-03-14T14:19:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [March 14, 2018, 2:19pm UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716/1 "2018-03-14T14:19:57Z")

</div>

I was expecting to have something like:

```julia
write(file, "varname1", variable1, "varname2", variable2)

```

Or even export all the data in the workspace in one batch?

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [March 14, 2018, 11:16pm UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716/2 "2018-03-14T23:16:23Z")

</div>

I have the following snippet in my program.

```julia
file = matopen("matfile.mat", "w")
write(file, "varname", variable)
close(file)

```

It seems that I have to delete the generated .MAT file, i.e. `matfile.mat`, to be able to re-run the code. `write` doesn’t overwrite, does it?

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [March 15, 2018, 11:31pm UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716/3 "2018-03-15T23:31:02Z")

</div>

Seems to overwrite fine for me:

```julia
julia> file = matopen("matfile.mat", "w");
julia> write(file,"foo",[1,2,3]);
julia> close(file);
julia> matread("matfile.mat")
Dict{String,Any} with 1 entry:
  "foo" => [1, 2, 3]

julia> file = matopen("matfile.mat", "w");
julia> write(file,"bar",[1,2,3]);
julia> close(file);
julia> matread("matfile.mat")
Dict{String,Any} with 1 entry:
  "bar" => [1, 2, 3]

```

---

<div class="post-metadata">

**Author:** ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)\
**Post date:** [March 15, 2018, 11:55pm UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716/4 "2018-03-15T23:55:30Z")

</div>

The package docs suggest

```julia
matwrite("matfile.mat", Dict(
	"myvar1" => 0,
	"myvar2" => 1
))

```

There is a `@save` macro in the JLD.jl package to save “everything” in the workspace using native format. You’d have to do some adapting though, saving only objects of types that MAT.jl can handle.

---

<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:** [May 14, 2020, 11:04am UTC](https://discourse.julialang.org/t/is-it-possible-for-the-mat-package-to-export-multiple-data-sets-using-a-single-call-of-the-write-command/9716/5 "2020-05-14T11:04:02Z")

</div>

You can use matwrite as the example in the Github, but instead of =\> 0 you write =\> variable.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd411d4a945eb09cf084448ba547786c1bbbc263.png)
