9
$\begingroup$

I would like to create a notebook populated with several modules and be able to evaluate these modules by running a separate notebook. I have attempted several uses of the NotebookEvaluate command with no luck. Can someone please explain to me how to do this?

More specifically, I would like to have a notebook named "functions.nb" which is populated with several functions, and evaluate this notebook in a notebook named "output.nb" which will call my written functions in "functions.nb", evaluate the functions, and provide the output.

As an example, if functions.nb contains:

Add[x0_,y0_]:=
Module[{x=x0,y=y0},
z=x+y
]

And output.nb contains:

Add[2,5]

The output would be:

z=7
$\endgroup$
3
  • 3
    $\begingroup$ Have you considered putting the definitions in a plain text file (package) and loading them with Get? $\endgroup$
    – Szabolcs
    Commented Mar 29, 2014 at 3:36
  • 1
    $\begingroup$ After reading about Packages I still can't figure it out. I made a simple package named "function.m" containing 'g[x_] := x^2' In another notebook I ran 'Get["function.m"]' but I receive a 'Get::noopen: Cannot open function.m. >>' error. What am I doing wrong? $\endgroup$
    – gKirkland
    Commented Mar 29, 2014 at 4:17
  • 1
    $\begingroup$ You either need to specify the full path to function.m or you need to change the working directory to where function.m is first. The working directory is not the same as the notebook directory. It is given by Directory[]. It can be changed by SetDirectory[]. SetDirectory@NotebookDirectory[] changes the working directory to the notebook directory (if the notebook is saved on disk). $\endgroup$
    – Szabolcs
    Commented Mar 29, 2014 at 16:18

2 Answers 2

9
$\begingroup$

I think the most straight forward would be to use Mathematica packages and importing the definitions in that notebook using the function Get as Szabolcs mentioned in the comments. I suggest that you have a closer look at the documentation on how to set up packages in Mathematica. Th principle is quite simple to understand.

Here is a small example of how you can set up a package. You first define your package and its context which is called functions in this case. Save the file as a Mathematica package file (.m). If you want to make a function to be available for other packages and notebooks, you can define a usage for that function in the exported symbols region. This will ensure that this function is available in the global context.

(* Mathematica Package *)

BeginPackage["functions`"]
(* Exported symbols added here with SymbolName::usage *)  

myAdd::usage="
myAdd[x0_,y0_]
computes z=x0+y0 and returns the result.
";
Begin["`Private`"] (* Begin Private Context *) 

myAdd[x0_,y0_]:=
Module[{z},
z=x0+y0
]

End[] (* End Private Context *)

EndPackage[]

You can then import the content of your package with the function Get and make the functions available in your Mathematica session. This will evaluate the definitions you made in the package functions will be available and you can use them.

Get["...pathToPackage...\\functions.m"]

By the way I recommend to not define functions that start with capital letters. All built-in functions start with capital letters and you might run into conflicts you don't want. Also you should have another look at local variables and the usage of Modules.

$\endgroup$
2
  • 1
    $\begingroup$ I got it working! Now I have 5 custom functions in a separate file and my script lengths is reduced by half! Thanks! I didn't realize I had to specify the path, that's why it was not reading the package. $\endgroup$
    – gKirkland
    Commented Mar 29, 2014 at 15:37
  • 1
    $\begingroup$ How do I do this without having to open and run the functions.m file in order to gain access to it? Also how do I make use of unassigned variables, e.g. bla[]:=x, without having them appear in the other notebook as functions`Private`x? $\endgroup$
    – H.v.M.
    Commented May 25, 2017 at 9:47
7
$\begingroup$

In the first notebook:

Add[x0_, y0_] := Module[{x = x0, y = y0}, x + y]
Save["myFunction", Add]

Or put your first notebook in to C:\Documents

In the second notebook:

Get["myFunction"]
Add[1, 2]
$\endgroup$
1
  • $\begingroup$ How can I replicate your answer above with CellTags rather than myFunction? I have various initialization cells in nb1 with CellTags and I like to recall these tagged cells from another notebook nb2. $\endgroup$ Commented Dec 6, 2019 at 22:49

Your Answer

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

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