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

Running Mathematica code using DOS

28 views
Skip to first unread message

Maria Davis

unread,
Mar 28, 2010, 5:07:45 AM3/28/10
to
Hi,

I want to run Mathematica with DOS Command. My purpose is to launch
a Mathematica program written in .nb file or in .txt file and to
execute it.
Can you please help me?
Many thanks.

David Bailey

unread,
Mar 28, 2010, 7:56:32 AM3/28/10
to
I assume you don't really mean DOS (which is a very old 16-bit operating
system), but the box you can open using CMD.

The easiest way to start a notebook or package file from within such a
system, is to type:

start foo.nb

or

start foo.m

(The start command uses the .nb suffix to recognise that the file should
be opened using Mathematica).

You wouldn't write a Mathematica program in a .txt file, but in a .m file.

BTW, I am a great fan of computing from the command line, and using
batch files, etc - which probably reveals something about my age!
However, I use a program called 4NT, which looks just like the standard
CMD box, but has additional features.

David Bailey
http://www.dbaileyconsultancy.co.uk

Maria Davis

unread,
Mar 29, 2010, 6:21:53 AM3/29/10
to
Hi David Balley
Thank you very much for your kind response.
I have created the file:"MYFILETOEXECUTE.nb" containing the code to execute.
I have created a new file named: "file.m" containing the following code:

UsingFrontEnd[
nb = NotebookOpen["MYFILETOEXECUTE.nb"];
SelectionMove[nb, All, Notebook];
FrontEndTokenExecute[nb, "EvaluateCells"];
]

I tried to use the command:
start file.m

But this command opens file.m, and to execute it, I must press the
button "Run Package" .

Is there a command that can execute automatically the package?

Thanks.

Zach Bjornson

unread,
Mar 29, 2010, 6:19:52 AM3/29/10
to
Or, if you need a bit more control or if your system doesn't associate
.nb or .m with Mmeca, you can do something like this:

MathKernel -noprompt -run "(*some command*);Quit[]"

In cases where you need to execute a file, set all of the cells as
Initialization Cells, then run

MathKernel -noprompt -initfile YourFile.m

where your notebook is normally YourFile.nb. There might be a more
glamorous way of doing that, but that works. (You will probably need to
use explicit/full file paths for both MathKernel and YourFile.)

Cheers,
Zach

dh

unread,
Mar 29, 2010, 6:20:58 AM3/29/10
to
Hi,
if you want to run a program under the kernel without the frontend
interface, you would write your input into a *.m file.
If you want to shutdown mma after the commands are executed, you end
your file with: Quit[]
To launch the Kernel and execute your commands:
...\MathKernel -noprompt -inputfile FileName
If the kernel is not on the Path, you need to give the full name,
enclosing it in quotes if it contains spaces. The same may apply the
input file name.
Daniel


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:d...@metrohm.com>
Internet:<http://www.metrohm.com>


dh

unread,
Mar 29, 2010, 7:56:30 AM3/29/10
to
Sorry, I made a typo. It should read:
-initfile
and not: -inputfile
Daniel

Maria Davis

unread,
Apr 4, 2010, 7:45:45 AM4/4/10
to
Thanks a lot for your help
I have tried MathKernel -noprompt -initfile file.m
But as result, a window named "Mathematica 7 kernel" is opened , and
it is empty.
Unfortunately, the file is not run.
Please can you help me?
Thanks.

dh

unread,
Apr 6, 2010, 7:24:39 AM4/6/10
to

Hi Maria,
my guess is that mathematica does not find your file. Did you give the
full name, including the path?
cheers, Daniel

0 new messages