Self(less) love - MATLAB* code, tips, tricks & tutorials for cognitive scientists.

Updated: May 20, 2019


You haven't been coding since birth? Shame!

Here's the truth, I basically didn't code at all during my PhD. I floundered around with MATLAB a bit at the very beginning, there was a dark week of Presentation scripting somewhere in the middle, but my supervisor didn’t really seem to code (or, if he did, his hands-off approach meant that I wouldn’t get much support) so I looked for shortcuts.


My first experiments required a lot of new stimuli so I spent about six months learning how to use DAZ 3D (a rendering studio for animators). Once the stimuli were made I had to tweak them all (literally ~800 unique jpegs) by hand in Photoshop. I became a DAZ and Photoshop ninja, but I couldn’t code myself out of a paper bag if my life depended on it. I could have spent the next six months trying to learn a programming language, but I just wanted to collect some data before my end of year upgrade. So, the reality is that when I started out all my tasks were created using E-Prime, my analysis was all done in SPSS and my figures (pretty much all line or bar plots showing the mean + SEM) were created in Excel or SigmaPlot.


Students, ECRs and anyone else who regularly feels inadequate, look me in the eyes and hear me:


There is no shame in using these tools. You are doing just fine.


Of course there are obvious advantages to coding things yourself (transparency, reproducibility etc) and now that I can code I encourage/support all my students to learn. However, an SPSS ANOVA and a bar plot does not de facto signal bad or questionable science. Bad science is about what you do with your data, not necessarily the tools you use.


Nonetheless, becoming a proficient coder felt like an insurmountable problem at times and if PhD student Becky had been on Twitter, with all its evangelising, debating, shaming and unsolicited advice, my self-esteem would have suffered for sure.





I only really started coding properly as a post-doc, thanks to patience, support and cheer-leading from @jonroiser. When you don't have a background in computer science, and you're trying to learn (how to code a task, analysis, simulation or model), Google is basically your best friend. A bunch of people have shared really useful code, examples and tutorials online and I wanted to bring these resources together in one place to help others find them.


So here it is, a (non-exhaustive) list of free MATLAB resources that I think are useful for cognitive scientists. In most cases these resources are provided with no benefit to whoever created and shared them, so you can use, amend, edit and change freely without citing anyone (but drop the kind folks a thumbs up on Twitter to say thanks if you feel so inclined).




Getting started


> Totally new to MATLAB? Antonia Hamilton's tutorial MATLAB for Psychologists is the place to start. I started here, and I advise you do too. Spend as long as it takes, do all the exercises. Trust me. Once you've worked through that, check out the slides and code from the ICN Matlab course.


fMRI


> For fMRI analysis, Steve Fleming has shared all his lab scripts for preprocessing, first and second level analysis in SPM. I would have literally given a kidney to have had this when I started my first fMRI project. In fact, he's generally got a lot of useful code on the MetaLab GitHub that's worth knowing about. Need to know how to do something specific in SPM, AFNI or Freesurfer? Andrew Jahn probably has a YouTube video tutorial for you.


Coding a task


> For programming an experimental task using the Cogent toolbox I have shared slides and (heavily commented) example code on my GitHub. This is a three part tutorial that takes you from the absolute basics (e.g. how to structure a task script, how to display one single image), up to your first full experiment with audio and visual stimuli, randomisation, response logging and RT calculation etc. The third part is for people who want to learn how to do more advanced things in Cogent Graphics. Check out the PowerPoint sides first, and follow along with the code examples.


Plotting


> For plotting, I have utilised the code that other people have shared (e.g. Anne Urai's violinPlot.m and Rob Cambells notBoxPlot.m) to make a flexible plotting function specifically for data from two groups. The vast majority of the data I collect involves two groups (and multiple conditions), so I wrote niceGroupPlot.m to produce a range of different kinds of plots (split violin, box&scatter or both) with easy options to change colours, labels, transparency and add linear fits. I wanted this to be AS EASY AS POSSIBLE TO USE. Examples and instructions are included in the download. One day I'll turn it into a GUI for people who can't code yet but find the plotting options in Excel/SPSS quite limiting.


Computational Modelling


> For behavioural modelling, Hanneke den Ouden and Jill O'Reilly have put together an amazing tutorial on how to fit different kinds of learning models (Bayesian and Reinforcement) to data from a reversal learning bandit task. It's a proper hand-held walk through of the theory behind Rescorla-Wagner learning and SoftMax response functions, with simulations and fits to real subject data. Let me tell you, Becky in 2012 spent a lot of time learning how to apply these sorts of models to study aversive learning in depression. My life would have been a lot easier if I'd had this tutorial to get me started! Also worth highlighting that the organisers behind the Zurich Computational Psychiatry Course have made all the code, slides and video lectures available. No substitution for attending the course (which is truly brilliant!), but incredibly useful.


> Analysis


Ok, so it's not a MATLAB tool, but if you're trying to wean yourself off SPSS then look no further than JASP. You'll need to cite this software if you use it, but its free to download. The graphical user interface will be familiar to anyone who has used SPSS before, but JASP provides you with Bayesian equivalents of t-tests, ANOVAs and correlations to supplement your frequentest results. There are many advantages to Bayesian statistics and you can learn more about that from these excellent JASP teaching materials aimed at undergraduates. I read somewhere once that JASP used to describe itself as a "low fat alternative to SPSS, a delicious alternative to R". I now use R a fair bit for data analysis (thanks to one of my brilliant MSc students who showed me the ropes - every day is a school day no matter what career stage you're at). A good introductory course about improving statistical inference, that also introduces R, can be found here. If you want to run your analysis using the MATLAB Statistics Toolbox but you find the help files a little hard to follow then there is an excellent introductory course (with code and examples) available on the iBM Lab GitHub.


That's all for now folks. I'll update and add to the list whenever I come across something useful and I promise that whenever I use a script or utility that I have coded I will try to clean it up, comment the code and share on GitHub.


Remember, it's OK to to be learning and it's OK not to be good at everything yet. Celebrate every fixed bug and minor programming victory with a high-five and a tea break, and only judge yourself against your past-self. If you're finding it hard to focus on actually doing science among the loud voices and circular debates on Twitter, just take a break from it, mute/unfollow the worst offenders and keep on swimming.


Further ahead in your career? Ninja programmer already? Just got strong opinions that you think 👏everyone 👏should 👏hear👏? When interacting on Twitter or IRL with colleagues (especially junior colleagues), I don't think its too much to ask for a little dignity, respect, courtesy and um, compassion.






--------------

* I'm close to conversational in R, and I can just about order myself lunch in Python when I need to, but I'm not really interested in being berated for still using MATLAB.


3,163 views

Follow me