I'm looking for a way to reference certain cells when opening a saved notebook. That is, assume I ran some commands in a notebook, got some output, saved the notebook, then reopened it in a new kernel and wanted point to information in specific cells.

As an example let's say that I save the following notebook contents

In[1]:= f[x_]:=g[x,h[x]]
In[2]:= f[3]
Out[2]= g[3,h[3]]
In[3]:= r[x_]:=f[x]+g[x]
In[4]:= r[3]
Out[4]= g[3,h[3]]+g[3]

which I then save. Upon reopening this notebook in a new kernel, all the information of the cells will be there in the respective cells and groupings, but (as far as I can discern from the documentation) with no easy method for referencing. If I wanted the g[3,h[3]] cell data before I closed the original kernel I could just use %2 or Out[2]. Is there a method to achieve this relative ease of cell referencing when reopening the notebook in a new kernel?

The reason I'm looking for this sort of functionality is I want to be able to detect when the output cells differ from their saved values when re-evaluating the notebook in a new kernel. I do this to ensure that custom functions from other notebooks/packages aren't exhibiting anomalous behavior when changes are made, basically checking the newest output matches the last known "good" output.

  • $\begingroup$ This isn't a direct answer to your question, but if you go to File -> New -> Testing Notebook you get a special type of notebook in which you can provide code snippets and expected results. Then when you want to make sure all the snippets still work, you just run this notebook. $\endgroup$ – C. E. Oct 1 '15 at 5:13
  • $\begingroup$ Have you looked at DumpSave? $\endgroup$ – Yves Klett Oct 1 '15 at 6:18
  • $\begingroup$ @YvesKlett I have toyed around with it a bit and the best I can seem to get out of it is saving all definitions, which at the least requires all my outputs be saved to a symbol. The greater issue though is as pointed out in this answer, the created file is platform dependent. This prevents me from relying on this method when doing verifications across my machines or when sharing notebooks with other user's in my group. Thanks for your suggestion; though if you know of a better way to use DumpSave than on the Global context, please share. $\endgroup$ – IPoiler Oct 1 '15 at 22:07
  • $\begingroup$ @Pickett Thank you for pointing that out, I had overlooked that document type. My goal is still to find a way to reference these cells though as my described notebooks usually don't take on the responsibility of being test suites until well after they become long and difficult to parse by hand/eye. I also suspect that a solution here may provide insight to another one of my open questions. $\endgroup$ – IPoiler Oct 1 '15 at 22:17
  • $\begingroup$ Regarding using DumpSave, could you just save Out instead of the entire Global context? $\endgroup$ – obsolesced Jun 25 '16 at 17:59

If you're willing to accept output cell indices being different, and semi-colon-suppressed output not being captured, ToExpression[#, StandardForm, Defer]& @@ NotebookRead @ Cells[CellStyle -> "Output"][[i]] should suffice. If output cell indices are to be the same as if input cells were evaluated sequentially from beginning to end of the notebook, create a list of cells from Cells[] with non-printing placeholders wherever there are input cells. I have no good solution for including references to semi-colon-suppressed output.

| improve this answer | |
  • $\begingroup$ Worth to mention that NotebookRead @ PreviousCell[] is giving boxes while % is an expression. $\endgroup$ – Kuba Jun 23 '16 at 9:47
  • $\begingroup$ @Kuba would ToExpression[#, StandardForm]& @@ NotebookRead @ Cells[CellStyle -> "Output"][[i]] be good enough? I can't think of a more direct way to convert cell objects to expressions than via boxes $\endgroup$ – obsolesced Jun 24 '16 at 4:24
  • $\begingroup$ I think that's a generic way (don't trust me too much), in case of Input cells you may want to add: ToExpression[#, StandardForm, Defer] & $\endgroup$ – Kuba Jun 24 '16 at 6:10
  • $\begingroup$ Yes that appears necessary. Will update. Thanks! $\endgroup$ – obsolesced Jun 24 '16 at 8:59
  • $\begingroup$ Was i intended to be an iterated value? $\endgroup$ – IPoiler Jun 24 '16 at 16:37

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.