Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
2  
Have you considered putting the definitions in a plain text file (package) and loading them with Get? –  Szabolcs Mar 29 at 3:36
    
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? –  gKirkland Mar 29 at 4:17
    
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). –  Szabolcs Mar 29 at 16:18

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  gKirkland Mar 29 at 15:37

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]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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