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.

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?

share|improve this question
3  
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. –  rm -rf Oct 12 '12 at 3:53
    
Do you want these functions to be defined in every notebook you open or only in this specific notebook? –  jenson Oct 12 '12 at 4:08
2  
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. –  Yves Klett Oct 12 '12 at 5:22
    
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. –  Sjoerd C. de Vries Oct 12 '12 at 12:09
    
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. –  rm -rf Oct 12 '12 at 14:09

1 Answer 1

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?

share|improve this answer
    
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. –  rm -rf Oct 12 '12 at 15:11
    
@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.) –  Mr.Wizard Oct 12 '12 at 15:26

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.