Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
See this post: mathematica.stackexchange.com/q/22584/363 –  Chris Degnen Dec 3 '13 at 19:58
add comment

3 Answers

up vote 17 down vote accepted

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

share|improve this answer
add comment

How about

Block[{Print = Sow},
  Do[Print[i], {i, 1, 1000}] // Reap
]
share|improve this answer
1  
In my case, re-running of the evaluation would take longer that manual copying of expressions from existing cells. –  Vladimir Reshetnikov Dec 3 '13 at 22:00
    
Yes sorry, I didn't read "(without re-running the evaluation)". –  RiemannZeta Dec 3 '13 at 22:03
add comment

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.

share|improve this answer
1  
Please see the OP's comment under @RiemannZeta answer. The nb has already been calculated. It's too late to change the code. –  belisarius Dec 4 '13 at 11:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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