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$
1

3 Answers 3

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$
2
  • 1
    $\begingroup$ In my case, re-running of the evaluation would take longer that manual copying of expressions from existing cells. $\endgroup$ Commented Dec 3, 2013 at 22:00
  • $\begingroup$ Yes sorry, I didn't read "(without re-running the evaluation)". $\endgroup$ Commented Dec 3, 2013 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
  • 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$ Commented Dec 4, 2013 at 11:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.