Showing posts with label mathematica pocket guide. Show all posts
Showing posts with label mathematica pocket guide. Show all posts

Saturday, November 24, 2012

Mathematica Keyboard Shortcuts

Keyboard Shortcuts and Handy Short Commands

Here are some useful and lesser-known keyboard shortcuts. In terms of saving time, rather than using brackets for single-argument functions, use Prefix: function@argument.

Likewise, nesting functions with increasingly distal matched brackets is difficult to read, increases programming time, and obstructs you highlighting what you feel is important in the function. Use Postfix-style with pure Functions like this:

dataFiles[[1]]//Rest//Flatten//Length;

dataFilesDifferences//Chop[#,10^-4]&//Union/@#&//ListPlot/@#&//Partition[#,3]&//Grid

As advocated in my 2007 User Conference talk (also here), a "Functional-Procedural Fusion" elegantly represents deeply nested functions in a readable manner. If you do use deeply nested functions, multiple-clicking the Head of an Expression will select its brackets.

Ctrl+F6          Switch between open Mathematica notebooks
F12                Toggle current notebook to and from full screen

Ctrl+Shift+K  After a function name gives you a template for the simplest form of the function
Ctrl+K            After a function name gives a drop-down listbox with autocompletions of the function
?*name*         As in "?*Plot*" lists all Mathematica functions with Plot in their name. You can then click on them to get quick help text.

?Global`*       Lists all Names created in current Mathematica session
F1                  Search Help for expression to left of cursor
Shift + F1      Open new instance of Help browser. Do scratchpad calcs in Help rather than opening a new Notebook. They're automatically cleaned up when you go to a new Help topic.

Alt+.              Interrupt a computation. If it doesn't work, do Evaluation/Quit Kernel.
Shift+Alt+.    Remove a selected computation from the Evaluation queue. This is handy when you have a long computation running, start another one, and want to abort that one without aborting the one that is running.

Rather than using palettes, just learn keyboard shortcuts for the symbols you use frequently, for example:

esc+[character}+esc  E.g. to give the Greek letter corresponding to the keyboard character
Ctrl+6             Superscript
Ctrl+-              Subscript
Alt+7              Format cell as plain text
Alt+6              Format cell as Subsubsection
Alt+5              Format cell as Subsection

%+Out line    As: %127. Refers to output line #127. Same as Out@127 or Out[127] but more concise. I use this frequently for a temporary variable name but if it turns out I need a more permanent mnemonic name I just create one: newName = %127. I use %, %%, or %2 only for very temporary "scratchpad" calcs.

Ctrl+L            Copy input cell from the one just above
Ctrl+Shift+L   Copy output cell from the one just above, into new input cell. An alternative is to just start typing in the Output cell and Mathematica automatically creates a new Input cell with those contents.

Clear@%127  Removes from memory some huge expression you created or imported in In[127].
ClearAll["Global`*"] and Remove["Global`*"] Clears or Removes all values from variables you created in the current session. If it comes to that, I usually kill the kernel though (Evaluation/Quit Kernel).

filePath="C:\\directory1\\directory2"; FileNameSetter@Dynamic@filePath






Gives you a in-Notebook button to click to navigate and select a filename or directory to which to Set filePath. Giving filePath an initial value before evaluating FileNameSetter takes you to that value (e.g. a directory) when you click the button. Using Dynamic lets you click the button over and over to re-Set filePath.

CreateDocument@expression Opens a new Mathematica notebook with expression in it. Expression can be an imported document.

Sunday, October 28, 2012

File Operations: Deleting Files


Delete one or more files

DeleteFile deletes one file, or more files if they are given in a List. If you are deleting more than one file, typically you'd first use FileNames to acquire those files from a directory. DeleteFile returns Null (i.e. nothing) if it works and $Failed if it doesn't. To delete a few files in the current directory it's as simple as this.

FileNames["test*", "C:\\Users\\82V\\Documents"] // DeleteFile

Here is a more complicated example in which I delete 3500 files in multiple directories.


Acquire FileNames and Delete Files in all subdirectories

The usual sequence of steps goes like this. Set the directory in which you want to search for files. In Windows, I usually use Windows Explorer to navigate to the directory, then click in the address bar to select it, Ctrl+C to copy it, and paste it after a quote mark in Mathematica, which says to me:



In[114]:= SetDirectory@
  "C:\\Users\\82V\\Documents\\Neuroscience\\Arle-Shils\\commandUNCuS\\\
DataFiles";

Search for all the files using a wildcard and store their names in a variable (datFiles). Search in all subdirectories within your directory by using a wildcard for subdirectory names in the optional second argument, and Infinity for depth of subdirectories in the optional third argument.

In[97]:= datFiles = FileNames["*.dat", {"*"}, Infinity];

See how many filenames you captured.

In[98]:= datFiles // Length

Out[98]= 3594

Take a quick peek at the first five files to be deleted to make sure you're capturing the right ones.

In[99]:= datFiles[[1 ;; 5]]

Out[99]= {"Short_Test_File\\tc125_101.dat", "Short_Test_File\\tc125_101pre.dat", \
"Short_Test_File\\tc125_102.dat", "Short_Test_File\\tc125_102pre.dat", \
"Short_Test_File\\tc125_103.dat"}

Delete all the files to be deleted.

In[100]:= DeleteFile@datFiles

Reset the directory to whatever it was before you set it to the one in which to search for files.

In[112]:= Directory[]

Out[112]= "C:\\Users\\82V\\Documents\\Neuroscience\\Arle-Shils\\commandUNCuS\\DataFiles"

In[113]:= ResetDirectory[]

Out[113]= "C:\\Users\\82V\\Documents"

Note that if you set the directory n times to delete files in each, you need to repeat ResetDirectory[] that many times to get it back to where it was, since each time you set a directory it pushes the previous ones down a stack (which you can reveal using DirectoryStack[]), and each time you use ResetDirectory[] it pops the directories back up the stack. You can use a Do loop on ResetDirectory[] n times to do this. Using the Print statement will show you the pop sequence of directories you have visited. I prefaced the command with Directory[] to show the current directory first.

In[117]:= Directory[]; Do[Print@ResetDirectory[], {3}]

C:\Users\82V\Documents\commandUNCuS

C:\Users\82V\Documents\Neuroscience\Arle-Shils\commandUNCuS\DataFiles

C:\Users\82V\Documents