I have a Mathematica Notebook with a function that i would like to be able to access from another Notebook without having to have the first Notebook open. The functions calls other functions in the same notebook so all code in that nootebook would have to be compiled. Thanks for any help in advance!!
Asked
Active
Viewed 180 times
2
1 Answers
4
Note that there is another, newer, way to store definitions that may suit your purposes: to store then as local objects (or even as cloud objects), then retrieve them when needed. I'll illustrate with a very simple example and just for local objects.
In a notebook session, evaluate successively:
def = "func[x_]:=Exp[-x]Sin[x]"
LocalObject["file:///Users/myname/Desktop/funcdef"]
Put[def, %]
Then at any subsequent Mathematica session, in any notebook, evaluate...
Get[LocalObject["file:///Users/myname/Desktop/funcdef"]]
ToExpression[%]
...and see that it works:
func[\[Pi]/4]
E^(-\[Pi]/4)/Sqrt[2]
murray
- 11,888
- 2
- 26
- 50
-
1Why not
Save/Getfrom a file directly? Is there any advantage of using LocalObject? There has to be a good one if one is expect to write their definitions as strings and use ToExpression later. – Kuba Jan 04 '19 at 08:02 -
@Kuba: I'm just pointing out an alternative there; no claim of advantage, although there may be one -- especially if you use a
CloudObjectrather than aLocalObject. – murray Jan 05 '19 at 15:45
Get. You may find auto-generated packages useful: https://mathematica.stackexchange.com/questions/1369/how-to-make-a-parallel-auto-generated-m-package-from-initialization-cells – Szabolcs Jan 02 '19 at 13:52BeginPackage, etc.Geting a file will simply evaluate everything in that file. It's as if you had evaluated a notebook. – Szabolcs Jan 02 '19 at 13:58