4
$\begingroup$

I want to efficiently keep my program in several documents, and initialise pieces of it automatically. I'm pretty sure that there is a manual on the subject, but I'm unable to find it on my own.

At first I've been splitting my code in several notebooks, and evaluating cells in them by hand, then I used an algorithm with SelectionEvaluateCreateCell to automate the process, and evaluate them as a batch. If this is a reasonable way to maintain one's code, then I have a specific question:

How to evaluate the cells in a specific order in the example below:

First notebook has a path "docpath/ExampleProcedure.nb", and there is one cell tagged "ETC" in it. This cell has the following code in it:

exVal = 11;

The second notebook has a cell (tagged "Caller") with a code:

exVal = 0;

nb = NotebookOpen["docpath\\ExampleProcedure.nb"];
NotebookLocate[{"docpath\\ExampleProcedure.nb","ETC"}];
SelectionEvaluateCreateCell[nb]

Print[exVal];

Now, what I want my algorithm to do, is to set exVal to 0, then open the first notebook and evaluate the cell tagged "ETC" in it, setting the exVal to 11, and then use Print on it. So the desired output is "11". Instead this code prints "0", and sets exVal to 11. Probably it first evaluates the cell "Caller", and only then evaluates the cell tagged "ETC", as opposed to just evaluating "Caller" line-by-line, and evaluating "ETC" as required to proceed through "Caller" line-by-line.

How to remotely execute code (cell tagged "ETC"), and only then proceed to next step (section containing Print[exVal])?

$\endgroup$
7
$\begingroup$

You possibly want to create one or more packages. Packages are special code files with extension .m or .wl, which load as code packaged (hence the name "package") in one or more namespaces. Mathematica has built-in support creating, managing, and loading packages.

A good place to start learning how to organize Mathematica code follow this link

| improve this answer | |
$\endgroup$

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