Monthly Archives: February 2013

Setting working directory in mathematica

One of mathematica annoyances when working with external files (various import/export functions), is that it defaults to the user's Documents directory. I usually keep the data/graphics files  in the same directory as the mmka notebook and its painful to specify the full path every time.

Here is a command that sets the working directory to the notebook directory.

SetDirectory[
DirectoryName[
ToFileName["FileName" /. NotebookInformation[SelectedNotebook[]]]]]

UPDATE: Easier (thanks Pedro!):
SetDirectory@NotebookDirectory[]

 

Tweaking Mathematica output for order-of-magnitude estimates

Here is a trick I find useful when quickly estimating order-of magnitude values of various quantities  in Mathematica. The statements below force the format of all numerical output to be in the scientific notation (x 10^y).

oldPost = $Post;
format[x_Real] :=
NumberForm[x, ExponentFunction -> (If[-1 < # < 1, Null, #] &)];
format[x_] := x;
$Post = format;

When you execute these statements, the output formatting will persist for the rest of the mmka session. If you want to go back to the default output format in the session, execute

$Post = oldPost;