Montag, 18. Juli 2011

Sharing data among Mathematica notebooks

With time my Mathematica-notebooks increase in size and complexity. That's when I decide to split my notebooks into several files. Also, sometimes I want my results out of the notebooks which possibly change upon each evaluation.

One way is to create a package stored to a notebook (.nb-file) which is convenient for function definitions. Another way is to write Mathematica expressions to an ASCII-file with the suffix ".m" with the command "Save".

Here, I have some expressions:

f[x_] := x Log[x]
a = Table[f'[i], {i, 0.1, 1, 0.1}]
p = ListPlot[a]
ascii = FromCharacterCode[Range[32, 126]]

With this command I can retrieve the names of the expressions in the "Global" scope, or context:

Names["Global`*"]

And finally, this is how I store all expressions which have names starting with "a" to a .m-file:

mfile = NotebookDirectory[]~StringJoin~"results.m"
Save[mfile, #] & /@ Names["Global`a*"]

In another notebook I can load these expressions like this

<<results.m;

The .m-file itself looks this way:

a = {62, 58, 46, 115, 47, 77, 110, 111, 72, 94}
ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

Keine Kommentare :

Kommentar veröffentlichen