How to Insert Custom Text with a Shortcut in PyCharm

When writing image analysis python scripts, or running simple things in the Python Console, I’m very often typing the same import statements over and over.

I thought it would be really great to have a shortcut to automatically insert the lines I normally use at the beginning of every script, and save myself a little bit of typing. This is possible to do in PyCharm, but wasn’t immediately obvious.

First, you need to “record” the text you want to insert as a macro:

  1. Edit > Macros > Start Macro Recording
  2. Type your desired text (in the console or editor). For me this was: import cv2 as cv
  3. Edit > Macros > Stop Macro Recording
  4. Give it a name.

Next, you need to assign your new macro to a shortcut:

  1. File > Settings > Keymap
  2. Under the Macros folder, you should see the name of the macro you just created
  3. Right click the macro & click “Add Keyboard Shortcut”
  4. Assign a shortcut (I chose F12)

That’s it! Now when you hit your shortcut in the console or editor it will auto-type your text! Easy!

How to Hide Specific Files or Folders from Surround SCM

It is possible to “officially” ignore files by extension, or ignore files & directories by name, but the option to do so is found under the “Server Options” of Surround which might be locked down by the administrator, and even if it isn’t, the changes would apply to all users which might not be desired.

However, I did stumble across one possible solution, (functional though inelegant), for just a single single client or user to set up a personal set of ignored files/folders.

All you have to do is set the files or folders as “hidden” in Windows Explorer. Then, Surround just ignores them. It’s sufficient to hide just a parent folder – the hidden attribute for any subfiles or subfolders won’t matter, Surround will ignore them all as long as the parent folder is hidden. Hidden files won’t show up anywhere in Surround, either when looking at “Working Directory Differences” or in the “Add Files” dialog.

While this isn’t as flexible as defining a set of rules (say, all files of a certain extension) it’s better than nothing.

How to Read Numeric Columns with read.xlsx2 in R

I like the xlsx package in R for its simplicity. However, I would always have a problem reading columns as numeric with the read.xlsx2 function. read.xlsx reads numeric columns properly, but it’s slow for large data sets.

By default, read.xlsx2 doesn’t guess the type of each column so it just reads everything as strings. It’s not immediately obvious from the documentation how to fix this without a bit of extra digging.

You actually have to look into the documentation of readColumns to see that if you pass colClasses=NA then it will guess the column type

So, use this:

data = read.xlsx2("path_to_xlsx_file.xlsx", sheetIndex=1, colClasses=NA)

How to Delete Scatter Plot Points From a Figure in Matlab

It is often useful to update the location of scatter plot points overlaid on a Matlab figure, especially when displaying the incremental results of iterative algorithms. In order to do this, the existing scatter plot points need to be deleted prior to redrawing new scatter points.

In order to delete points which have been plotted on an axis with scatter(), the following code can be used:

delete(findobj(gca, 'type', 'patch'));

It is not immediately obvious from the documentation, but scatter points are considered to be a “patch” type object. Thus, this line of code finds all patch objects on the current axes and deletes them.

To delete the 2D lines drawn by Matlab’s plot() function a similar statement can be used, in this case by changing the type to the more obvious value of “line”:

delete(findobj(gca, 'type', 'line'));

How to Create a Random UUID in Matlab

Generating a universally unique identifier (UUID) can be very useful for a variety of tasks where you need a unique way to identify something. While a UUID is not technically guaranteed to be unique, the identifier space is so large (128-bit) that it can be treated as unique for most purposes.

It is simple to generate a UUID string in MATLAB by using its built in Java Virtual Machine:

uuid = char(java.util.UUID.randomUUID);

The function java.util.UUID.randomUUID() will return an object of type java.util.UUID, but this can be typecasted into a MATLAB string through the char() function. Simple!