9
$\begingroup$

In MATLAB, I can save and recall my own function using an M-File(function or script)
How can I do something similar in Mathematica?

I already searched and know how to define functions and modules like the following:

f[x_]:=Sin[x]+Cos[x]  
g[x_]:=Module[{a}, a*f[x]]

When I restart Mathematica, however, my own function is deleted.

Of course, I can save my notebook(.nb) file, but it is just saving my script, not saving my definition of the function.

How can I deal with this problem?

$\endgroup$
5
  • 3
    $\begingroup$ Look up Put, PutAppend, Save, DumpSave and Get in the documentation. That should give you enough to get started. There are several ways to do it in Mathematica — initialization cells, Put/Get, and in more advanced cases, using packages. In your case, the function definition (as you've written it) should still be available in the saved notebook (assuming you didn't delete the cell); just that the kernel doesn't know about it. All you need to do then is to reevaluate the cell. $\endgroup$
    – rm -rf
    Commented Oct 12, 2012 at 3:53
  • $\begingroup$ Do you want these functions to be defined in every notebook you open or only in this specific notebook? $\endgroup$
    – jens_bo
    Commented Oct 12, 2012 at 4:08
  • 2
    $\begingroup$ The workflow in Mathematica is slightly different. By default, your code cells are not evaluated automatically when you open a notebook. You can have a look at the use of initialization cells and packages which allow for automated evaluation. $\endgroup$
    – Yves Klett
    Commented Oct 12, 2012 at 5:22
  • $\begingroup$ mr.wizard That answer is focussed at definitions involving an Interpolation. I believe the OP is confused about MMA's execution model and Yves' answer in the comment above is spot on. $\endgroup$ Commented Oct 12, 2012 at 12:09
  • $\begingroup$ I agree with Sjoerd and acl. All that the OP is looking for here is for the subtle difference between MATLAB and Mathematica. In MATLAB, saving the function/script file ==> function is in your workspace, but not so in Mathematica. Mr.Wizard's answer definitely answers all of the save/load questions and should be linked to, but the OP just needs a simple clarification of the difference here. $\endgroup$
    – rm -rf
    Commented Oct 12, 2012 at 14:09

1 Answer 1

7
$\begingroup$

I gave a fairly complete description of the methods available to save and load function definitions in this answer.

You can use these methods to save your definitions to an .m file, and then load the definitions from this file with Get, shorthand <<, e.g. << myfile.m.

Alternatively you can place all your definitions in one or more cells given the property Initialization Cell through the Cell > Cell Properties menu. When you open the Notebook you will be asked if Mathematica should evaluate these cells for you. These cells can be Closed (deselect Open in the menu above) to hide them.

You could also combine these methods, placing all your definitions into a .m file, and then place a single Needs or Get statement in an Initialization Cell in your Notebook. If you want to load these definitions upon starting Mathematica you can place the Needs statement into your Kernel/init.m file located in $UserBaseDirectory.

See:

Automatically load packages at startup

Programmatically generate packages from notebook files?

What are recommended guidelines for developing packages?

$\endgroup$
2
  • $\begingroup$ Like I said in the comment, your answer there fully covers these aspects of it, so there's no need to repeat it. What the OP needs is an explanation of how the MATLAB workspace is different from the Mathematica workspace. Please edit/rephrase your answer to address that. $\endgroup$
    – rm -rf
    Commented Oct 12, 2012 at 15:11
  • $\begingroup$ @rm sorry, I've never used Matlab (shocker). I guess you'll have to answer this one. (FWIW my referenced answer does not discuss Initialization Cells, package creation, and init.m, so I don't know what you mean about repeating it.) $\endgroup$
    – Mr.Wizard
    Commented Oct 12, 2012 at 15:26

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.