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.
1 Answer
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!
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$