1

I have results of some calculation as a number of output cells in the saved .nb file. Since these results are obtained before (not during this run of Mathematica), I cannot use % to access previous output. They are many so I cannot do this manually. I need to extract the results from the output cell into a single list automatically. How can I do this?

user15933
  • 173
  • 7

2 Answers2

2

You should be able to use NotebookImport. For example, here is a notebook saved as "example.nb":

nb = CreateDocument[{
    ExpressionCell[Defer[2+2],"Input"],
    ExpressionCell[4, "Output"],
    ExpressionCell[Defer[SparseArray[RandomInteger[10,10]]],"Input"],
    ExpressionCell[SparseArray[RandomInteger[10,10]],"Output"]
}];
NotebookSave[nb, FileNameJoin[{$TemporaryDirectory,"example.nb"}]]

Then, using NotebookImport:

NotebookImport[FileNameJoin[{$TemporaryDirectory,"example.nb"}], "Output"] //InputForm

{4, SparseArray[Automatic, {10}, 0, {1, {{0, 10}, {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}}, {5, 8, 2, 3, 7, 1, 8, 1, 9, 4}}]}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
0

Due to some reason,

NotebookImport[file, "Output"]

didn't work. It returned an empty array. I used

NBI=NotebookImport[file];

And then

ToExpression[NBI[[i]]]

in the loop.

user15933
  • 173
  • 7
  • What does NotebookImport[file, _ -> "Cell"][[All, 2]] return? Is "Output" not one of the possible cell styles? – Carl Woll May 15 '19 at 22:20