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

I was expecting to have something like:

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

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

1 Like

I have the following snippet in my program.

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?

Seems to overwrite fine for me:

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]

The package docs suggest

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.

You can use matwrite as the example in the Github, but instead of => 0 you write => variable.
image