1
$\begingroup$

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?

$\endgroup$
0

2 Answers 2

2
$\begingroup$

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}}]}

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ What does NotebookImport[file, _ -> "Cell"][[All, 2]] return? Is "Output" not one of the possible cell styles? $\endgroup$
    – Carl Woll
    May 15, 2019 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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