11
$\begingroup$

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?

$\endgroup$
18
$\begingroup$

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

$\endgroup$
2
$\begingroup$

How about

Block[{Print = Sow},
  Do[Print[i], {i, 1, 1000}] // Reap
]
$\endgroup$
  • 1
    $\begingroup$ In my case, re-running of the evaluation would take longer that manual copying of expressions from existing cells. $\endgroup$ – Vladimir Reshetnikov Dec 3 '13 at 22:00
  • $\begingroup$ Yes sorry, I didn't read "(without re-running the evaluation)". $\endgroup$ – Chip Hurst Dec 3 '13 at 22:03
1
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Please see the OP's comment under @RiemannZeta answer. The nb has already been calculated. It's too late to change the code. $\endgroup$ – Dr. belisarius Dec 4 '13 at 11:44

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.