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

question

#1

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?


#2

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?


#3

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]

#4

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.