1
$\begingroup$

(I use Mathematica 9.0.1 on OS X 10.10.2.)

Suppose I have a notebook in which I make some definitions and assignments.

a = 108
b[x_] := 4x + 8
c = Interpolation[{15, 16, 23, 42}]

Then it is straightforward to save these definitions to a text file:

Save["filename.txt", {a, b, c}]

produces a text file filename.txt containing

a = 4

b[x_] := 8*x

c = InterpolatingFunction[{{1, 4}}, {4, 3, 0, {4}, {4}, 0, 0, 0, 0,
          Automatic}, {{1, 2, 3, 4}}, {{15}, {16}, {23}, {42}}, {Automatic}]

But is there a way to produce exactly the above content, not in a text file, but in an ".nb" file?

(Note that the obvious first guess, just changing the extension of the filename I specify in the Save command from ".txt" to ".nb", does not seem to work. Mathematica still treats the file as a text file, rather than a notebook file, it just now has a deceptive file extension.)

$\endgroup$
3
$\begingroup$
a = 108
b::usage = "test usage msg"
b[x_] := 4 x + 8
c = Interpolation[{15, 16, 23, 42}]

As pointed by OP, the direct usage of FullDefinition produces boxes that are not realy useful. I don't know how to convert them easily without this tiny undocumented function [1], [2]:

Save["test.m", {a, b, c}]

CreateDocument @ Cell[#, "Input"] &@
 First @ FrontEndExecute @ 
   UndocumentedTestFEParserPacket[Import["test.m", "Text"], False]

enter image description here


Old answer, not so handy, in edit history.

$\endgroup$
  • $\begingroup$ Try evaluating the cell, saving and closing the file, and reopening it. $\endgroup$ – thecommexokid Mar 26 '15 at 20:52
  • 1
    $\begingroup$ @thecommexokid again good point, sorry for not being focused enough :) is my edit ok? :) $\endgroup$ – Kuba Mar 26 '15 at 21:56
  • $\begingroup$ Yes, it is, thanks. It's really weird to me that Save["test.m"] produces a proper .m file, but Save["test.nb"] just produces a text file with a misleading .nb extension. $\endgroup$ – thecommexokid Mar 27 '15 at 17:59
  • $\begingroup$ By the way, not sure why you deleted all your previous comments! I don't think that was really necessary... $\endgroup$ – thecommexokid Mar 27 '15 at 18:01
  • 1
    $\begingroup$ @thecommexokid Try to wrap it with UsingFrontEnd. I will be afk for 1h now. $\endgroup$ – Kuba Mar 30 '15 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.