11

I have a notebook where evaluation of a certain cell produced hundreds of adjacent output cells using Print. I need to create a list containing all expressions from those output cells as elements, and assign the list to a variable without re-running the evaluation.

Could you suggest a way to automate this process to avoid manually copying all expressions?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Vladimir Reshetnikov
  • 7,213
  • 27
  • 75

3 Answers3

18

The following seems to work, however I think it's not general enough:

At a clean nb, enter:

For[i = 0, i < 4, i++, Print[{i, {33, i}}]]
For[i = 0, i < 4, i++, Print[Graphics[Circle[], ImageSize -> 20]]]

Mathematica graphics

And then retrieve the Print[ ] output as:

c = Cases[NotebookRead /@ Cells[GeneratedCell -> True], Cell[___, "Print", ___]];
ToExpression /@ Cases[c, BoxData[__], {2}]

Mathematica graphics

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
2

How about

Block[{Print = Sow},
  Do[Print[i], {i, 1, 1000}] // Reap
]
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136
1

Make each result the argument of a pure function; e.g., (Sow[#]; Print[#])&[whatever], with all the functions inside a Reap. That also lets you format the printed output in a way that might be easier for a person to read but more awkward to read back into Mma.

Ray Koopman
  • 3,306
  • 14
  • 13
  • 1
    Please see the OP's comment under @RiemannZeta answer. The nb has already been calculated. It's too late to change the code. – Dr. belisarius Dec 04 '13 at 11:44