How to save an matrix to use in a separate script

Hi All,

Apologies if this has been asked, however, I haven’t been able to find a simple answer. I have a matrix of solutions which I would like to save and then load into another script for plotting and some analysis. My solutions are the output of Method of Lines package, if this helps. I’m sure there is something simple, in Python I would use pickle, but not sure what the Julia equivalent is?

Thanks for your help!

1 Like

I would recommend BSON:

using Pkg
Pkg.add("BSON")
using BSON: @save, @load

@save "path/to/file.bson" your_data

Then to load from the other file:

using BSON: @save, @load

@load "path/to/file.bson" your_data
1 Like

There are lots of file formats. You can use text formats like CSV, binary formats like the above-mentioned BSON or HDF5, or more Julia-specific formats like JLD; see the links for how to read and write them in Julia. Probably JLD or JLD2 is a good choice for interchanging data between Julia scripts; if you want to exchange data with other software I would generally use CSV or HDF5.

1 Like

Thanks both, I think JLD is what I was looking for!

Hi, the next code using JLD2 could be usesfull:

using Pkg
using JLD2
function save_Array_JLD2(nameIN::Array{}, prefixfile::String)
        # Save nameIN object into prefixfile.jld2
        namefile=prefixfile*".jld2"
        save_object(namefile, nameIN)
    end
export save_DF_JLD2
#
function load_JLD2(prefixfile::String)
        # Load in memory the object from prefixfile.jld2
        namefile=prefixfile*".jld2"
        return load_object(namefile)
end
1 Like