User Tools

Site Tools


Module 3: Visualizing neural data in MATLAB (short version)


  • Load local field potential (LFP) and spike data from an example data set
  • Use the MultiRaster() function to plot and navigate through loaded raw data
  • Understand which file format to export figures to


Loading some data

First, make sure you have the folder R042-2013-08-18 from the data share, and that this is placed in a sensible location (NOT in a GitHub or project folder!).

This data set was collected (by Alyssa Carey for her Master's thesis work) from the dorsal CA1 region of the hippocampus of a rat performing a T-maze task, using a drive with 16 independently movable tetrodes. Spike and LFP data was recorded from each tetrode; possible spike events were detected online and stored for offline spike-sorting, and LFPs were sampled at 2kHz and bandpass filtered between 1-475Hz. (A quirk of this particular data set is that certain time intervals are cut out of the spike data, but not the LFP. So you may notice some odd looking gaps in the rasterplot later.)

Use the shortcut button created in the previous module to set your path. In your project folder, create a folder with today's date and cd to it. This will be your “scratch” working directory with stuff that you don't necessarily intend to push to GitHub.

Create a sandbox.m file in your daily folder. In this file, use cell mode to load some spike trains, a LFP, and position data as follows (recall you can use Ctrl+Enter to execute the code in a cell):

%% cd to data folder -- replace this with yours
fc = 'D:\data\DataAnalysisTutorial\R042-2013-08-18';
%% load the data (note, may need to unzip position data first)
cfg = [];
S = LoadSpikes(cfg)

If you are using your own data that has a missing or bad .clusters file, do this instead:

cfg = [];
cfg.useClustersFile = 0;
S = LoadSpikes(cfg)

This should give a ts data structure containing spike train data:

LoadSpikes: Loading 67 files...
S = 
        t: {1x67 cell}
    label: {1x67 cell}
      cfg: [1x1 struct]
      usr: [1x1 struct]

The details of this are discussed in Module 2. In brief, each cell of S.t contains the spike times from a putative neuron. The qualifier “putative” is used because this is extracellular data and spike-sorting is not perfect, so it's likely there will be some spikes missing and some spikes included that are not from this neuron. Always remember this even if I will omit the “putative” from now on for short. You can see we have loaded 67 neurons for now.

Let's also load a LFP:

cfg = [];
cfg.fc = {'R042-2013-08-18-CSC03a.ncs'};
csc = LoadCSC(cfg);

This gives:

LoadCSC: Loading 1 files...
LoadCSC: R042-2013-08-18-CSC03a.ncs 0/17193 bad blocks found (0.00%).
>> csc
csc = 
     tvec: [8802816x1 double]
     data: [1x8802816 double]
    label: {'R042-2013-08-18-CSC03a.ncs'}
      cfg: [1x1 struct]

csc is the common Neuralynx designation for “continuously sampled channel” and typically is an EEG or LFP type signal sampled and filtered so that high-frequency components such as spikes are not accessible. It is possible to have wide-band, 32kHz CSCs suitable for spike extraction, but these are not included in the current dataset. As discussed in Module 2 a LFP tsd is defined by matching sets of sample timestamps (csc.tvec) and sampled data (

Plotting basic spike rasters

The S variable that contains our spike times is a MATLAB struct that conforms to the ts (“timestamp”) format introduced in Module 2. Its .t field contains spike trains as lists of numbers, corresponding to the timestamps of the spikes. So, to plot a raster with the spikes of the first cell in our data set:

nCell = 1;
SET_SpikeHeight = 0.4;
plot([S.t{nCell} S.t{nCell}],[nCell-SET_SpikeHeight nCell+SET_SpikeHeight],'k')
set(gca,'YLim',[-10 10]);

☛ Extend the example code above to make a rasterplot containing the spikes for all cells in S. To facilitate re-use later, wrap your code in a function, PlotSpikeRaster, that plots the raster for any ts input.

You should find that it takes a significant amount of time to plot that many spikes. In practice we often want to plot spikes for specific time intervals; we will do this below. Alternatively, there exists code that is much faster at plotting rasters (albeit more obscure; implemented in PlotSpikeRaster2() if you want to see an example).

Temporally restricting ts and tsd objects

A useful function that works on ts, tsd and iv objects is restrict().

☛ Use MATLAB's help function to look up the usage information for restrict(). In a new editor cell, use this function to create a S_r variable with data restricted to between 5950 and 6050 s.

Calling your PlotSpikeRaster function on this restricted data should be much faster. Now that we are a bit more zoomed in, some interesting features of the rasterplot become visible. For instance, at time 5967, many cells are synchronously active. Could this be a sharp wave-ripple event (SWR)?

☛ Restrict the csc to the same time (5950 to 6050) and plot it in the same figure. You can use the rescale() function to fit the csc into a convenient data range (e.g. rescale(,-5,0)).

You should see that the synchronous activation around time 5967 is indeed associated with a striking oscillation in the LFP:

This high-frequency osillation (the “ripple”) superimposed on a slower oscillation (the “sharp wave”) is the signature associated with “replay” events in the hippocampus. We will explore this phenomenon in detail in later modules.

Exporting figures

A good way to save figures you may want to keep is:


Notice that the first argument of print is the current figure (gcf). The other arguments specify that we want a PNG file, with 300dpi resolution. PNG format is a good choice for saving figures because it uses lossless compression, JPG images, which use lossy compression, can have ugly artifacts (but more colors). Other useful save formats are -dill which saves .ai Illustrator files, and -depsc which saves an embedded PostScript file (both vector formats).

☛ Critically evaluate the formatting of figure shown above. What are some missing elements you would want to see if this figure were to be used in a talk or paper? What changes could you make to improve legibility?

MultiRaster() is a versatile and fast plotting function, with many different options and features.

The most basic usage is simply:


Note that the first argument in this case is the empty matrix []. For more features, this argument is used as a cfg variable with several options, for instance:

plot_cfg = [];
plot_cfg.lfp = csc;
plot_cfg.spkColor = 'hsv';

This particular data set also has some experimenter annotations associated with it, for example in the ExpKeys file:

>> LoadExpKeys
>> ExpKeys
ExpKeys = 
                Behavior: 'MotivationalT'
         RestrictionType: 'water'
                 Session: 'standard'
                  Layout: 'foodLeft'
                Pedestal: 'R'
                nPellets: 5
             waterVolume: []
                 nTrials: 18
            forcedTrials: []
    nonConsumptionTrials: []
               badTrials: []
              pathlength: 318
                patharms: 369
           realTrackDims: [139 185]
                convFact: [2.9176 2.3794]
             TimeOnTrack: 3240
            TimeOffTrack: 5645
               prerecord: [2.1266e+03 3.2141e+03]
                    task: [3.2387e+03 5.6452e+03]
              postrecord: [5.6564e+03 6.5635e+03]
                 goodSWR: {1x2 cell}
               goodTheta: {'R042-2013-08-18-CSC07a.ncs'}

The meaning of these fields is explained in more detail in the full task description, but for now we will just use the start and end times for the 'prerecord' segment of the data to highlight it visually:

plot_cfg = [];
plot_cfg.evt = iv(ExpKeys.prerecord);
plot_cfg.evtColor = [0 0 0.5];

MultiRaster() is interactive, allowing the user to scroll, zoom, move from one event to another, et cetera. To see the full list of keybindings, type help navigate.


buy tadalafil us, 2021/06/02 04:48

tadalafil gel tadalafil pills <a href=“”>buy tadalafil us</a>

what is tadalafil, 2021/06/02 08:17

tadalafil online tadalafil tablets <a href=“”>tadalafil 40 mg daily</a>

what is tadalafil, 2021/06/02 08:40

buy tadalafil us tadalafil 40 mg from india <a href=“”>tadalafil 40 mg daily</a>

tadalafil 40 mg daily, 2021/06/02 09:41

generic tadalafil united states tadalafil dosage <a href=“”>tadalafil</a>

buy tadalafil us, 2021/06/02 10:17

generic tadalafil 40 mg tadalafil 40 <a href=“”>tadalis sx</a>

what is tadalafil, 2021/06/05 08:52

tadalafil gel tadalafil 40 mg daily <a href=“”>tadalafil 40 mg daily</a>

tadalafil gel, 2021/06/06 15:44

tadalafil 40 mg daily tadalafil 60 mg for sale <a href=“”>tadalafil 40</a>

tadalafil tablets, 2021/06/06 21:46

tadalafil max dose 40 mg tadalafil <a href=“”>tadalafil 40 mg from india</a>

what is tadalafil, 2021/06/07 00:23

tadalafil 40 tadalafil tablets <a href=“”>tadalafil 40 mg daily</a>

tadalafil 40 mg from india, 2021/06/07 06:59

buy tadalafil tadalafil 40 <a href=“”>tadalafil daily use</a>, 2021/12/13 06:34

3D ART PRINTS————————————-

3D ART PRINTS https:

DonaldWiplY, 2022/01/07 19:19

cialis daily medication <a href=>cialis 47</a> cialis price australia online tadalafil 20mg <a href=“”>cialis dapoxetine</a> tadalafil citrate 20mg

Herbertmit, 2022/01/09 20:16

cialis 800mg black <a href=>tadalafil 10mg cost</a> cialis 100mg dosage viagra and cialis <a href=“”>tadalafil gel</a> what cialis does

WilliamStary, 2022/01/12 13:00

6odlio <a href=>sildenafil 200mg price</a> o7lje5 <a href=“”>viagra amazon</a> ahyfpz <a href=>viagra goes generic</a> v978lk <a href=“”>200 mg sildenafil</a>

Erichneuro, 2022/01/13 05:41

sildenafil 100mg generic <a href=>viagra goes generic</a> over counter viagra 100mg sildenafil coupon <a href=“”>citrate sildenafil</a> sildenafil tabs 50mg

Curtisbus, 2022/01/14 19:50

viagra savings <a href=>viagra gel</a> canadian viagra pharmacy viagra discount coupon <a href=“”>generic for viagra</a> goodrx viagra

Ceciltuh, 2022/01/15 16:02

sildenafil citrate generic <a href=>men viagra</a> sildenafil tablets uk viagra for women <a href=“”>viagra discount coupons</a> viagra for girls

TimothyEdile, 2022/01/17 08:11

cialis black pills <a href=>cialis coupon 2019</a> cialis 50mg tadalafil price walmart <a href=“”>cialis pharmacy coupon</a> cialis 800mg black

JimmyVox, 2022/01/20 05:21

zithromax 500 mg <a href=>azithromycin cost</a> zithromax capsules 250mg azithromycin 500 mg tablet <a href=“”>zithromax dosing</a> zithromax tablets

Robertbum, 2022/01/23 06:41

cialis generico <a href=>cialis 5mg singapore</a> generic medications cialis cialis everyday <a href=“”>cialis price increase</a> buy cialis

KennethFep, 2022/01/25 20:48

strengths of meloxicam <a href=>mobic medication dosing</a> porphyria and meloxicam meloxicam high <a href=“”>meloxicam 15 mg dosage</a> mobic medication for uti

Marcusmon, 2022/01/26 20:24

levitra cialis viagra <a href=>cialis overdose</a> cialis efectos secundarios cialis 500 <a href=“”>cialis 60mg</a> does cialis work

Scottweevy, 2022/01/28 09:41

viagra cost comparison <a href=>viagra wikipedia</a> 100mg sildenafil price viagra walmart <a href=“”>sildenafil tablets</a> cheap indian viagra

Rickybep, 2022/01/29 07:31

sildenafil prices <a href=>best viagra generic</a> hims sildenafil viagra for women <a href=“”>rx sildenafil tablets</a> women taking viagra

Michaelzer, 2022/02/01 18:43

tadalafil prescription cost <a href=>tadalafil price uk</a> free trial cialis cialis average price <a href=“”>cialis canada pharmacy</a> otc tadalafil

Dominicmaf, 2022/02/02 17:24

viagra cream <a href=>virectin vs viagra</a> viagra rx coupon 100mg sildenafil price <a href=“”>viagra price</a> buy sildenafil citrate

Arielle, 2022/06/26 06:01

Spot on with this write-up, I really believe this amazing site needs far more attention. I'll probably be returning to see more, thanks for the information! save refuges

save refuges, 2022/06/26 06:01

Spot on with this write-up, I really believe this amazing site needs far more attention. I'll probably be returning to see more, thanks for the information! save refuges

RTL live stream kostenlos ohne anmeldung, 2022/09/09 16:58

RTL live stream kostenlos ohne anmeldung

live tv, 2022/09/20 17:43's%20horse%20racing%3C%2Fa%3E%3C%2Fh1%3Eörse%20ntv%20live&url=ählt%20RTL%20now&url=ählt%20RTL%20now&url=ç%20izle%3C%2Fa%3E%3C%2Fh1%3Eädt%20nicht%3C%2Fa%3E%3C%2Fh1%3Eühstücksfernsehen%3C%2Fa%3E%3C%2Fh1%3Eühstücksfernsehen%3C%2Fa%3E%3C%2Fh1%3Eüll%20und%20tränen%3C%2Fa%3E%3C%2Fh1%3E's%20dance%202021%3C%2Fa%3E%3C%2Fh1%3E's%20dance%202021%3C%2Fa%3E%3C%2Fh1%3Eßball%20kostenlos%3C%2Fa%3E%3C%2Fh1%3Eüll%20und%20tränen%3C%2Fa%3E%3C%2Fh1%3Eönül%20dagi%20canli%20izle%3C%2Fa%3E%3C%2Fh1%3Eßball%3C%2Fa%3E%3C%2Fh1%3Eün%3C%2Fa%3E%3C%2Fh1%3E's%20tv%20racing%3C%2Fa%3E%3C%2Fh1%3E's%20love%20RTL2%3C%2Fa%3E%3C%2Fh1%3E's%20love%20RTL2%3C%2Fa%3E%3C%2Fh1%3E's%20horse%20race&url='s%20dance&url='s%20dance&url=übertragung&url=österreich%3C%2Fa%3E%3C%2Fh1%3E's%20dance%3C%2Fa%3E%3C%2Fh1%3E's%20dance%3C%2Fa%3E%3C%2Fh1%3Eün%3C%2Fa%3E%3C%2Fh1%3Eüll%20und%20tränen%20heute%3C%2Fa%3E%3C%2Fh1%3Eösterreich%3C%2Fa%3E%3C%2Fh1%3E

Enter your comment. Wiki syntax is allowed:
analysis/nsb2016/week3short.txt · Last modified: 2018/07/07 10:19 (external edit)