Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

export to .m using a cell command

29 views
Skip to first unread message

Roberto

unread,
May 18, 2012, 5:29:25 AM5/18/12
to
Hello,

I know how to make a .m file using the "Save As" menu. This forces every time to choose the folder where I want to put it and the file name.

As I update frequently my .m I was wandering if there is a way to make this using a cell command.

I tried something like

Export["~/my.m", EvaluationNotebook[]]

but the .m created does not contain the current notebook, but it's rather empty.

What should I use to save all the cells in the current notebook into a .m?

Thanks in advance for your replies.
Cheers
Roberto



djmpark

unread,
May 19, 2012, 5:48:07 AM5/19/12
to
Use the Option Inspector, Shift+Ctrl+O.
Show option values for your notebook.
Select Notebook Options, File Options.
Set AutoGeneratedPackage to Automatic.

Then the first time you save your notebook a .m file will be created from
your initiation cells. After that every time you update your notebook the .m
file will also be updated.

Check out:

?AutoGeneratedPackage.

If your notebook is primarily a package notebook you could work directly
with the .m file and skip the .nb file. You can use Section headings and
Text cells in the .m file just as in the .nb file. The principal difference,
from the .nb file, is that the package code is in Code cells, which are
Initialization cells, and not in Input cells. You can switch the Style of a
Cell to Input if it is more convenient to edit in that form. (This is
especially the case with Usage messages.) You could use Input Cells to save
old inactive versions of code.

You can create a new .m file from the Mathematica menu, File, New, Package,
give it whatever name you wish, and copy or write code into it.

It is sometimes convenient to do development and testing in a regular
notebook and then copy the code to the .m file. This is the usual way to
work within Workbench but you can also do it without Workbench.


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html

Roberto Franceschini

unread,
May 19, 2012, 5:44:33 AM5/19/12
to
Hello David,
The AutoGeneratedPackage option was clearly what I need to be pointed at.

Working on the .m directly is harder as this is notebook on which I do some testing as well, but I shall consider this for the future.

Thanks for the tips!

Cheers
Roberto

Chris Degnen

unread,
May 23, 2012, 3:29:21 AM5/23/12
to
Another way to write out the notebook as a package, (without using initialisation cells), is to run the following:-

FrontEndExecute[FrontEnd`AddMenuCommands["Save",
{MenuItem["Save As Pac&kage",
FrontEnd`KernelExecute[
nb = SelectedNotebook[];
SelectionMove[nb, All, Notebook];
str =
First[FrontEndExecute[
FrontEnd`ExportPacket[NotebookSelection[nb], "InputText"]]];
If[# =!= $Failed,
Module[{name},
name = StringDrop[#, -3];
Export[name <> ".txt", str];
Quiet[DeleteFile[name <> ".m"]];
RenameFile[name <> ".txt", name <> ".m"]
]
] &@Quiet[NotebookFileName[nb]]
],
MenuKey["G", Modifiers -> {"Control", "Shift"}],
System`MenuEvaluator -> Automatic]
}]]

It will place a command on the menu which can be run with Alt-F, K or Shift-Control-G. When run, it will write out the current notebook as a package, provided the notebook already exists as a saved file, ie has a filename.

You can read more about ExportPacket here:

http://mathematica.stackexchange.com/questions/1319/how-do-i-extract-the-contents-of-a-selected-cell-as-plain-text/1411#1411

The notebook text is first saved as a .txt file and then renamed because directly exporting as a .m file doesn't save the formatting so well.

0 new messages