I provide two ways:
1) Human readable
data1 = RandomInteger[100, {25, 25}];
data2 = RandomReal[100, {25, 25}];
Save["humanReadable.m", {data1, data2}];
Unset[{data1, data2}]
Get["humanReadable.m"];
Dimensions@{data1, data2}
{2, 25, 25}
Note, that you can dump many different variables with ease, and the file is in easy to read Mathematica syntax allowing all kinds of symbolic and numeric data without any manual serialisation. By default Save
appends which can be convenient, but ofc. must be kept in mind.
Saving the data from other sources in such format might be very handy, as you can use all of the Mathematica syntax including comments. For example I use it for measurement control software data dump.
2) Platform independent binary
Obviously the upside of binary format is smaller file size and loading time in case of big data.
In addition to mx
data format already discussed, there is version and platform independend format wdx
. Again there is a way to export and import it with symbol names attached.
DumpSave["platformIndependendBinary.wdx", {data1, data2}];
Unset[{data1, data2}]
Get["platformIndependendBinary.wdx"]
And if you don't want to fix / remember the variable names you can
DeleteFile["platformIndependendBinary.wdx"]
Export["platformIndependendBinary.wdx", {data1, data2}]
Unset[{data1, data2}]
{data1, data2} = Import["platformIndependendBinary.wdx"];
The same method works for .m
files for human readable text format.
The only downside of wdx
compared to mx
is speed.
Export
/Import
in "MX" format). This is fast, and does not really involve serialization / parsing in the usual sense (via strings). In other words, mx files bypass the high-level parsing, populating internal structures at lower level. In addition, mx files preserve packed arrays. $\endgroup$