User Tools

Site Tools


This is an old revision of the document!

Module 1: Setting up


  • Set up a working MATLAB installation with appropriate path shortcuts
  • Use GitHub to acquire the analysis code we will use
  • Perform some elementary GitHub operations (pull, add, commit, push) and create a branch for your project
  • Create a well-designed folder structure for your project, and be aware of naming conventions (in process, promoted data)
  • Connect to the lab database, download a data set, and test your path setup



Installing MATLAB

If you are using a lab computer, it will have MATLAB installed. Verify that it can start successfully (you'll get the » prompt in the Command Window).

These modules assume a basic working knowledge of MATLAB, corresponding roughly to the material in the "Interactive MATLAB tutorial". If you are unsure, take a look at the table of contents. If there are things you don't recognize, work through the tutorial. If you prefer a different format, you can use Chapter 2 of the MATLAB for Neuroscientists book to get up to speed.

If you are totally new to MATLAB, or even to computer programming in general, don't hesitate to ask questions, we are here to help you!

Regardless of your MATLAB abilities, two great ways to keep learning are:

  • Cody, a continually expanding set of problems with solutions to work through, with a satisfying points system to track your progress
  • MATLAB questions on StackOverflow, a Q&A site where you can browse previous questions and add new ones

Setting up GitHub

GitHub is a system for “distributed version control”: it keeps track of changes to a set of files, such as pieces of MATLAB code, with one or more contributors. This system makes it easy to keep track of evolving code share improvements between collaborators. A typical use for this is if you want to run the exact code that you used to generate some figure a while ago, but you've since made changes to the code; or the same analysis suddenly gives a different result and you want to track down what change caused it. If you are new to GitHub, you can watch the video under Resources above to get an overall idea of how it works and why it is useful.

If you don't already have a GitHub account, go to GitHub and sign up. E-mail me (mvdm at dartmouth dot edu) your account name, so I can give you access to the code repository.

Meanwhile, download and install the Git client of your choice if you don't already have one installed. For Windows, I recommend GitHub Windows as a user-friendly way to get started. For installing Git and setting up GitHub on various operating systems, see GitHub: Set Up Git; note that on the NS&B computers, you need to select “Run as administrator” when running the installer.

Next, configure your client. For GitHub Windows, after starting up the GitHub GUI (the default window that opens when you run GitHub) you'll first need to sign in with your account, then click Tools > Options. Set the “Default Storage Directory” to something reasonable such as D:\My_Documents\GitHub\). Also check that your username and e-mail address look ok (I am mvdm).

Cloning the NS&B codebase

Next, we will use Git to create a local copy (“clone”) of the NS&B codebase. If you are already familiar with git and have its binaries on your shell path, you can simply do git clone, which will create a new folder nsb2015 in your working directory. If this is your first time using it, try navigating to the course repository website. There, click the button “Clone in desktop”, and your GitHub client should prompt you to accept the clone (tested successfully with Chrome, YMMV). If this fails, you can drag the URL from your browser into your GitHub client. As a last resort, you can try to add the Git binaries to your path, opening a shell (command line, cmd.exe), and typing the git clone command above. If that doesn't make sense, ask!

Now, verify that you have created a nsb2015 folder with various subfolders and files in it, indicating that you have a local copy of the codebase. Because Git is tracking the contents of this folder, it is now easy to “pull” the latest version from GitHub, either from the command line:

git pull

Or, by clicking the Sync button in the GUI.

This “pull” should do nothing, because you already have the latest version. The basic idea is that you can stay up-to-date easily as well as contribute to the codebase so that everyone else can benefit. As you might expect, that part is known as a “push”, which we will do in the next step.

A first commit and push

Open the file in the nsb2015 folder. The .md extension is for Markdown, a lightweight set of commands to format text (syntax reference is here).

Add your name to the list and save the file. Then go to your git shell (open one with the gear icon in the top right of your client's GUI) and type git status. Git has noticed the change, but it says that this change is not yet “staged for commit”. In other words, git is not tracking this file. Let's fix this:

git add
git commit -m "Added name to list in README file"

If you now do a git status you will see that you are ahead of the origin (the online repository) by 1 commit. This makes sense because you just made a change. Let's push this by doing git push. If you get an “access denied” type error, email me (mvdm at dartmouth dot edu) your GitHub username and I will give you permission. If everything goes to plan you should now be able to see the updated README file on GitHub. As above, you can also use the GUI Sync button to accomplish the same steps (albeit in a less transparent manner).

A schematic of these basic operations (pull, commit, push) is shown below, using the amazing DokuWiki plugin for GraphViz:

What happens if in between your pull and push someone else pushes a change? In that case you cannot push your changes unless you do a pull first and resolve any conflicts. In any case, you should always do a pull first before attempting to push.

Using GitHub to acquire the FieldTrip toolbox

Using your experience from the previous section, create a local clone of the FieldTrip toolbox. If you are using the command line, make sure that you cd to your GitHub folder, i.e. that you are not within some other project such as nsb2015, before cloning. If things worked correctly you should have fieldtrip and nsb2015 folders within your GitHub folder; not a fieldtrip folder within your nsb2015 folder!

We will use this toolbox extensively for the analysis of local field potentials. Be aware that it is about 1.2GB!

Note how using GitHub to obtain FieldTrip not only ensures you have the most recent version, but also enables you to easily incorporate future changes, revert to previous versions, etc. using pull and other git tools.

Configuring MATLAB to use the code from GitHub

Open MATLAB and create a shortcut titled something like “NS&B 2015 - Hippocampus”. The code for the shortcut should be

restoredefaultpath; clear classes; % start with a clean slate
cd('D:\My_Documents\GitHub\nsb2015\code-matlab\shared'); % or, wherever your code is located
p = genpath(pwd); % create list of all folders from here
cd('D:\My_Documents\GitHub\FieldTrip'); % or whatever you chose, obviously

This ensures that whenever you click this button, you have a clean path (the set of folders, other than the current working directory, whose contents MATLAB can access) of only the MATLAB default plus your local versions of the two GitHub repositories.

Optional: if you don't like the .git folders in your path, you can get clever with regular expressions to remove these:

p = regexprep(p,'D.*?\.git.*?;','');

Establish a sensible folder structure

So far, you have local GitHub repository clones added to MATLAB's path. But as you work on your project, you will write your own analysis code. You will also have data files to work with; some that you download as part of these modules, and some that you will perhaps collect yourself. It is important to consider where all of these files will go, and how you will manage them. I recommend using three separate locations:

  • GitHub folders. Files in here you only change (or add) when you can improve what is already there. This content is backed up and version-controlled (i.e. you can see the complete history of changes and revert to any version you want) through the GitHub system. These files can be shared by multiple different projects, including working through these modules, analysis related to the data you collect, and perhaps a PhD project! For me, this folder is in D:\My_Documents\GitHub\.
  • Project folders. Each project has a home folder which holds the code for that project. As explained in the Noble paper, it is a good idea to create a new folder for each day you work on the project. If you find you are copying certain functions or snippets of code from day to day, those should be moved to the shared folder. It is critical that the contents of this folder are backed up in case of computer failure. I use Dropbox for this, so an example project folder I have is D:\My_Documents\Dropbox\projects\vStrGammaProbe\.
  • Data folders. Data, both raw and preprocessed, should live in a different place: D:\data\ in my case. This is because different projects may access the same data, and because backup strategies for data are typically different than for code.

With this trifold division, when you want to work on a project, you would click the appropriate MATLAB shortcut for it first. Following the example above, this should add the appropriate GitHub folders to the path. Next, the shared folder of the project is also added to the path. Data is generally not added to the path, because some data files in different folders may have the same name. Then, you create a new folder with today's date, and you are ready to go!

There are several situations when it is appropriate to move code from your project folder to a GitHub folder:

  • you improve a piece of code that was already on GitHub
  • you have a new piece of code in the shared project folder that is proving useful
  • you reach a milestone, such as an analysis that tests a certain hypothesis

If you are an owner or collaborator of a GitHub repository, you will be able to push changes you make. I can enable this for you on the NS%B repository, but to be accepted as a collaborator on a large project such as FieldTrip, you will need to show your work to the owners first (as can be done by creating a Fork or branch and and issuing a pull request).

Grab a data session from the lab database

If you can access the NS&B share (usually Z:\), an example data folder can be found in the NSB_2015\4_MouseHippocampus\DataAnalysisTutorial\data folder. For this module you will only need the R016-2012-10-08 folder. A good place to put it is in something like D:\data\promoted\ (Rxxx indicate different rats, followed by the date of each session). In general you want to keep your data separate from your code; for instance, multiple analysis projects may use the same data, so you don't want to duplicate it.

The choice of the folder name promoted indicates that these are data folders for which preprocessing is completed. As explained further in Module 2, preprocessing typically includes the renaming of raw data files, annotation, spike sorting, and a few other steps. In general, it is useful to keep promoted data separate from data still in process.

If you cannot access the NS&B share, use a FTP client such as Filezilla to connect to the lab FTP server, mvdmlab-nas1 ( Configure your FTP client to require “explicit FTP over TLS” and use BIOL680 as username and password.

Correct FileZilla configuration is the following:

If you cannot log in to the server, send me your IP address and I will enable access for you.

Verify things are working

As explained in the Noble paper, create a folder with today's date in your project folder. Create a sandbox.m file in it, click your previously made shortcut to set up the paths, and use Cell Mode to check that you can load a data file:

%% load data
cd('D:\Data\R016\R016-2012-10-08'); % replace this with where you saved the data
cfg = [];
cfg.fc = {'R016-2012-10-08-CSC02d.ncs'}; % cell array with filenames to load
csc = LoadCSC(cfg);

When you execute the above cell (Ctrl+Enter when it is selected in the Editor), you should get:

LoadCSC: Loading 1 files...
LoadCSC: R016-2012-10-08-CSC02d.ncs 44/10761 bad blocks found (0.41%).
>> csc
csc = 
     tvec: [5498360x1 double]
     data: [1x5498360 double]
    label: {'R016-2012-10-08-CSC02d.ncs'}
      cfg: [1x1 struct]
>> csc.cfg
ans = 
      history: [1x1 struct]
          hdr: {[1x1 struct]}
      ExpKeys: [1x1 struct]
    SessionID: 'R016-2012-10-08'

What you have loaded is in fact a local field potential recorded from the rat ventral striatum. The different file types and data fields above will be explained in more detail in the next module. For now, let's just take a peek at the data:

xlim([1338.6 1339.2]);

You should see some interesting oscillations – we will explore these in detail in upcoming modules. If you see this, you have successfully completed this module!

Folder and file naming


For Mac/OS X users

If you are running MATLAB on OS X (and possibly Linux), the above sandbox.m code will may fail. The following steps have worked for someone using OS X 10.8, with MATLAB R2013a:

  • Head over to the Neuralynx website.
  • Download the Neuralynx to MATLAB Import for Linux and Mac OS X package (direct link).
  • Extract the archive you have downloaded into a folder, and add that folder to your path shortcut.
  • Navigate to the extracted folder/binaries/, find the file Nlx2MatCSC_v3.mexmaci, and rename it to Nlx2MatCSC.mexmaci (removing _v3)
  • Again, make sure this folder is included in your path, and try running the sandbox.m again.
  • If you add neuralynx above nsb2014 in your path MATLAB should use the new neuralynx binaries. If not, you may need to delete nsb2014/util/neuralynx/ for this to work.

The sandbox.m should run properly now, and you should see the plot you're supposed to see.

For Linux users

Follow the instructions above for Mac/OS X users, except you may need to recompile the binaries (note that you will need C and C++ compilers installed. Install the build-essential package on Ubuntu):

  • You may want to just delete the existing binaries.
  • Edit to set PLATFORM=64PC or PLATFORM=32PC depending on your architecture, and edit INCLMATLAB and BINMATLAB so that they point to the correct directories for your Matlab installation. If you don't remember, run locate mexsh in the shell and you should see the path.
  • You can rename all the files in the binary directory with the shell command:
    > rename 's/_v3//' *

This worked on 64-bit Ubuntu with Matlab R2013b.

analysis/nsb2015/week1.1437357523.txt.gz · Last modified: 2018/07/07 10:19 (external edit)