Sunday, March 23, 2014

iPython: clear current line

When you have some junk on the command line, and want to clear it, many interpreters (e.g., Matlab) would use <ctrl>-c. In the iPython command line, <shift><esc> will clear it up (while <ctrl>-z will undo your most recent action). This may seem strange, but it is nice that the clear command no longer competes with Copy, which is also typically <ctrl>-c. If you really wanna go crazy, <ctrl>-l clears the entire screen.

Saturday, February 15, 2014

Saving an entire project in Code::Blocks

You are working with projectx in Code::Blocks, and want a copy of  the entire project with all its files
(e.g., header files). You try to save your project by entering File-->Save Project As, but it only saves a single cbp file. What do you do? You could copy over all the files individually, but that would be very time consuming for complex projects.

A more efficient method exists, but is not obvious from the Code::Blocks menu system. It involves two easy steps: 
1. Save template of projectx 
With projectx open, click File-->Save Project as Template and enter whatever name you want for this template. 
2. Open a new project from that template 
Click File-->New-->From Template, then pick the name you entered In Step 1. After clicking Go the program will request a folder to place the project. When it asks if you want to change the project name, you should do so unless you want it to have the exact same name as the template from Step 1. 

That's it. Those steps should give you an exact duplicate (potentially with a different name) of projectx.

Sunday, December 15, 2013

List comprehensions in Python

I was thinking of writing a post about the topic, but discovered an excellent introduction to list comprehensions that is about the level I was going to pitch it (An Introduction to List Comprehensions in Python). Highly recommended.

Why am I writing about Python at a neuroscience blog? Because of Brian.

Wednesday, December 04, 2013

Getting output from a Matlab GUI

Let's say you have a GUI like the one on the right as part of a program that runs a mouse in a simple behavioral task. The GUI requires the user to manually enter some data (the animal's name) and verify that the power is on in your setup. We want the GUI to close and return the relevant outputs when the 'Start Program' button is pressed by the user.

There are three tweaks you will need to make this work, if you made your GUI using the GUIDE functionality in Matlab (note if you want to tinker with an example, I've included one at the end of this post).

1) Make the GUI wait before it returns outputs
The GUI will try to return outputs right when it is invoked, well before any of the values can be specified by the user. To block such behavior, you can pause program execution using the uiwait command within OpeningFcn:
This will make the GUI wait until some additional action is performed (e.g., uiresume is called to resume program flow, or the GUI (with handle hObject) is closed). This gives the user time to enter the actual values.

2) Specify the output variables
Within OutputFcn, use varargout to specify what outputs you want returned from the GUI. Something like:
 varargout{1} = handles.data1; 
 varargout{2} = handles.data2;
Matlab typically passes information among the different elements of a GUI using the handles variable, so this code just exploits this fact. Once the outputs are specified as above, they will be returned as outputs to the calling function for the main GUI:
 [data1, data2]=GUI_Practice; 
Where GUI_Practice is the name of the m-file that defines the GUI.

Caveat: you will probably define the desired outputs (such as handles.data1) within a callback function (using something like handles.data1=x). When you do so, be sure to enter the following within the callback function:
guidata(hObject, handles); 
This saves the local variable handles to the GUI handle, so they will not be annihilated outside the scope of the callback function.

3) Tell the program when to resume
If you only did the above steps, after calling uiwait the program would hang indefinitely. You need to call the uiresume command to bump the program out of wait mode. To resume program flow when the user clicks Start Program, add uiresume to CloseRequestFcn:
%When user clicks button, check to see if GUI is in wait  %mode. If it is, resume program; otherwise close GUI
 if isequal(get(hObject, 'waitstatus'), 'waiting')
Note: you should also add the  line delete(hObject); to the end of outputFcn. Otherwise, the user will have to attempt to close the GUI twice: once to resume program flow with uiresume, and again to close the GUI with delete.

Below is a simple example called GUI_Practice (m-file and fig file are both needed for this to run, as it was made with GUIDE). Once the files are in your Matlab path, you can instantiate the GUI by entering animal_name=GUI_Practice;

I got some of the ideas for this from Mathworks (here). If you are having trouble getting it to work, let me know in the comments, and I'll try to help.


Saturday, April 06, 2013

Matlab question marks and exclamation points

Random Matlab things I find cool or perplexing. Updated periodically. Some of the comments are very dense, basically just lines of code I will likely forget, but will want to remember at some point.

To check what mfile is currently running, enter mfilename (useful in debug mode).

1. Filtering an image stored in matrix M:
%build the filter to convolve with the image
%convolve them
2. To change your gridlines to solid grey without changing the colors of the tick labels:
%make gridlines solid
%make them grey
set(gca,'Xcolor',[.8 .8 .8],'Ycolor',[.8 .8 .8])
%unfortunately, the above changes everything to grey

%copy the axes
%redo them in black. 
set(c,'color','none','xcolor','k','xgrid','off', ...

If your Windows machine doesn't show the .mat file extension (and you have already unclicked 'Hide extensions for known file types' in your folder options menu) you can fix it within an open folder. First, select Tools->Folder Options->File Types-->New. A GUI to create a new extension will open: type MAT in the field. Then click 'Advanced' and select Matlab Data from the list. It will warn you that this is already associated with a different file type. Accept the change. Problem solved. I stole this simple solution here, and Matlab has a page about it here.

1. If you have a cell array that contains strings, and want to get a numeric array with 1's where a particular string occurs, and 0's otherwise, you can use the cellfun function coupled with strfind: 
>>out=~cellfun('isempty', strfind(cell_array,'string'));

2. Why doesn't the following yield a 1?

You can use plotyy to display data on different y axes in the same figure. While there isn't presently a scatteryy command (why?), you can try something like the following:

1. It would be cool if, on a documentation page for a function, it let you click on a 'function history' link that showed when the function was introduced, and the changes added with each version.

2. Check out the grpstats function. Enter your data, and the group assigned to each data point, and it calculates all sorts of statistics sorted by group (e.g., mean, standard error, standard deviation, etc). I had done this on my own, but their function is better than what I had.

3. Why isn't the following legal?
>>scatter(x,y,'Color',[a b c])
Why must we use CData (and not Color) for scatter plots?

Saturday, May 12, 2012

What is a p value?

A p-value is a number associated with a statistical test. There are two things you need to just know to understand p-values (enough to get you through a paper that throws around the term, anyway).

First, statistical tests look for differences between two quantities. For instance, is the mean height of men different from the mean height of women? The null hypothesis is that the two things being compared are the same.

The second thing to understand is the p-value itself. The p-value is the probability that the two quantities are indeed the same. That is, how likely is it that your data would look the way it does if you assume the null hypothesis is true?

For instance, let's say you randomly select 20 men and 20 women, and the mean height of the men in your sample is five foot ten, and the mean height of the women is five foot seven. Would such data demonstrate that men (on average) are taller than women, or could the observed height difference simply be due to chance? This is what the p-value tells you. It tells you the probability that men and women have the same mean height, given what you observed in your experimental sample.

Two corollaries:
a. If the p-value is very high (e.g., 0.99), that means it is likely that the two quantities you are comparing are the same, i.e., that the null hypothesis is true. E.g., males and females are the same height.
b. If the p-value is very low (the convention is below 0.05), this suggests that the two quantities you are comparing are truly different, i.e., that the null hypothesis is not true. E.g., the two means are different.


For the mavens:  technically the p-value is the probability of observing what you did, or a more extreme value, but that is a wrinkle that makes things too complicated for a dirty synopsis.

Friday, January 20, 2012

Filtering images in Matlab

Given image stored in matrix M:

%build the filter to convolve with the image
%convolve them