A simple non-graphical user interface in Matlab : keyboard callback functions

I’m not the first person to be annoyed by Matlabs guide (a tool used to generate GUIs that, unfortunately, are difficult to understand and painful to modify afterwards). Some months ago, I was looking for a way to implement a lightweight user interface for analyzing big data sets, particularly to mark ROIs in calcium imaging movies. I found a simple way which does not use any buttons or any other graphical user interface elements, but only relies on keyboard callback functions. Most likely, this programming style is useful for other tasks as well.

In a first step, a figure is opened ; next, the figure gets a keyboard callback function, with keys assigned to certain tasks. E.g. if I press “f”, I can draw a freehand selection of a ROI ; if I press “x”, the data set presentation switches from anatomy to an average dF/F presentation ; if I press “m”, the overlayed ROIs increase their contrast. And so on. This works like a charm and keeps the programming part short, almost without any scripting overhead for the user interface part.

Another example : I have e.g. 179 timetraces of neurons, and I want to manually assign them to be either “type A” or “type B”. So I use a for-loop to plot each timetrace ; once a timetrace is plotted, I use the keyboard callbacks of the “rightarrow” and “leftarrow” to manually sort the timetrace to the “A” or “B” bin. This is the fastest way I know to manually sort data, and again it works with keyboard callback functions for Matlab.

That’s how it looks like in real code:

Part 1:

global clusterType
nb_neurons = 179;
timetraces = rand(nb_neurons,4000);
for kk = 1:nb_neurons
   handle1 = figure(2198); plot(timetraces(kk,:));
   set(gcf, 'WindowKeyPressFcn', {@chooseCluster,kk,handle1});
   waitfor(gcf);
end

Part 2:

function chooseCluster(~,event,kk,handle1)
  global clusterType;
  clear keyword
  keyword = event.Key;
  switch(keyword)
     case 'leftarrow'
       clusterType(kk) = 1;
     case 'rightarrow'
       clusterType(kk) = 2;
  end
  disp(num2str(kk));
  close 2198;
end

The value for each trace (1 = type A, 2 = type B) is assigned to the variable clusterType. This is one of the few occasions when it actually does make sense to use global variables in Matlab.

The next step towards a ROI analysis user interface would be to use a key callback to execute a code line like “x = ginput(1)” or “h = imfreehand(gca)”, allowing for selection of locations and ROIs in the image.

If you want to enhance the experience of this non-graphical kinds of user interfaces, check out the function akZoom(), to be found at fileExchange@mathworks. It allows you to pan and zoom in your figure with mouse gestures, and it can easily be modified according to your needs.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s