Here is the solution which I currently use. It isn't perfect but serves quite well for my purposes.
In the Notebook I define an auxiliary function which takes a variable where a data array is stored and prints an initialization Cell
with two-dimensional human-readable representation of that array which will be assigned to the input variable on initialization:
ClearAll[printInitCell];
SetAttributes[printInitCell, HoldAll];
printInitCell[array_] :=
CellPrint@Cell[
BoxData[RowBox[{ToString[Unevaluated[array]], "=",
GridBox[Map[ToBoxes, array, {-1}],
GridBoxFrame -> {"Columns" -> {{True}}, "Rows" -> {{True}}}], ";"}]], "Output",
InitializationCell -> True, Evaluatable -> True];
Here is an example of use:
dataset1 = Prepend[RandomReal[1, {5, 3}], {"Head1", "Head2", "Head3"}];
printInitCell[dataset1]
It is important that after initialization the variable dataset1
contains two-dimensional array which is not wrapped by Grid
:
The only drawback of this approach is that when I need to re-generate the initialization Cell
, I have to remove the previous Cell
manually.
UPDATE
With AutoIndent -> False
and the variable placed above the data table the data Cell
s are more readable:
ClearAll[printInitCell];
SetAttributes[printInitCell, HoldAll];
printInitCell[array_] :=
CellPrint@Cell[
BoxData[RowBox[{ToString[Unevaluated[array]], " ", "=", " ", "\n",
GridBox[Map[ToBoxes, array, {-1}],
GridBoxFrame -> {"Columns" -> {{True}}, "Rows" -> {{True}}},
BaseStyle -> (FontWeight -> Plain)], ";"}]], "Output", InitializationCell -> True,
Evaluatable -> True, AutoIndent -> False, Background -> GrayLevel[0.95],
FontWeight -> Bold];
Now
UPDATE 2
Here is Mathematica 8 - compatible solution which auto-overwrites previous version of the data Cell
(if present) before printing of the new version.
At first, it checks whether the next Cell
is a previous version by comparing the CellTags
. If the latter is true it sets the Evaluatable -> False
option what turns on automatic overwriting. Then it prints the new version and collapses the CellGroup
hiding everything except to the newly printed initialization cell.
ClearAll[printInitCell];
SetAttributes[printInitCell, HoldAll];
printInitCell[var_] := With[{tag = ToString[Unevaluated[var]], nb = EvaluationNotebook[]},
SelectionMove[nb, After, EvaluationCell, AutoScroll -> False];
SelectionMove[nb, Next, Cell, AutoScroll -> False];
If[CurrentValue[NotebookSelection[], CellTags] === {tag, "CellType=DataCell"},
SetOptions[NotebookSelection[], Evaluatable -> False]];
CellPrint@
Cell[BoxData[
RowBox[{ToString[Unevaluated[var]], " ", "=", " ", "\n",
GridBox[Map[ToBoxes, var, {-1}],
GridBoxFrame -> {"Columns" -> {{True}}, "Rows" -> {{True}}},
BaseStyle -> (FontWeight -> Plain)], ";"}]], "Output",
InitializationCell -> True, Evaluatable -> True, AutoIndent -> False,
Background -> GrayLevel[0.95], FontWeight -> Bold,
CellTags -> {tag, "CellType=DataCell"}];
SelectionMove[nb, Previous, Cell];
FrontEndTokenExecute["OpenCloseGroup"]];
Button
to import the data and generate an assignment cell that is output below that. $\endgroup$"Input"
cells with theImport
statements as showed on the screenshot. $\endgroup$Import
statements must not beInitialization
cells because I wish do not depend on the external files. At the same time, if the external files are changed it should be possible to regenerate the cells with the data byImport
ing the external files. So the output cells are generated automatically by Mathematica but manually in the sence that they are notInitialization
cells. Sorry for confusion. $\endgroup$