2
$\begingroup$

I have multiple nb files with function definitions and sample usages inside. I need to extract only user defined functions and same them into a new file as a library of functions. I have looked at NoteBookOpen, NoteBookEveluate, FullDefinition... But I don't know how script the process of opening a nb file, find all functions, save them to a new file. Please no ridicules or flames or useless comments such as "read the documentation" or "google it". Just code samples, please.

$\endgroup$
2
  • $\begingroup$ Mathematica gives you a large number of ways of defining functions. Your problem will be easier if you consistently define your functions in simple and recognisable ways. It would help to include a few lines of Mathematica that you wish to include and a few that you wish to exclude. $\endgroup$
    – mikado
    Aug 19, 2016 at 22:41
  • $\begingroup$ You could clear the global context, open and evaluate a notebook, use Save[somefilename, "`*"], and then do it all again for the next notebook. Check the Save documentation to see if that's what you are looking for. $\endgroup$
    – Karsten7
    Aug 20, 2016 at 0:03

1 Answer 1

1
$\begingroup$

It looks like you are looking for an automatic way to extract the user defined functions.

I don't have an answer for that but there is a semi-automatic way that requires a bit of up-front work.

After defining a user-defined function, select the cell, go to the cell menu, select cell properties and click on Initialization Cell, shortcut is ctrl-8.

Do this for all the functions you want to save.

In the notebook, go to the options inspector (Format -> Options Inspector, shortcut is shift-ctrl-O.

At the set the Show option values toggle to Selected Notebook.

Under Notebook Options -> File Options set AutoGenerated to Automatic.

Now when you save a notebook it will create for you a separate text file with the name (notebookName.m).

You can re-start mathematica with a fresh notebook and open that file.

Then you can copy and paste the function definitions from that file into the fresh notebook and save it.

This may look like a lot of work, but writing and reading it takes longer than doing it.

Good luck!

$\endgroup$

Your Answer

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

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