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?
2 Answers
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}}]}
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.
-
$\begingroup$ What does
NotebookImport[file, _ -> "Cell"][[All, 2]]
return? Is "Output" not one of the possible cell styles? $\endgroup$ May 15, 2019 at 22:20