
What is MATLAB and why do you need it?
It's a popular programming language that scientists use to design and run research experiments.
Moreover, when conducting research in psychology or neuroscience, you get huge amounts of data and with MATLAB, you can easily collect, import, analyse and visualise it.
Most degrees in psychology and neuroscience have a programming module as a part of their curricula.
However, the majority of students have zero coding experience when starting to study. That's what happened to us when doing MSc in Clinical, Social and Cognitive Neuroscience. We had no idea how to write code for our research project and had to start from scratch. There wasn't much information out there and learning coding from textbooks can be quite hard. We wanted to have someone explain to us the basics of programming in MATLAB so we could follow along.
Since we couldn't find a short online course tailored for neuroscience and psychology students and researchers, we decided to make one ourselves after finishing our degrees.
So in this course, we will try to explain the basics of getting started with MATLAB for your experiments in a way that we wish we were taught when we started studying and working on our research.
We strongly advise starting with Mathworks video tutorials to learn about setting up MATLAB on your device and to get familiar with common terminology.
Then, you can also get textbooks from the library or online, we recommend reading 'MATLAB for Behavioral Researchers' by C.R. Madan and 'Programming Behavioral Experiments with MATLAB and Psychtoolbox' by E. Misirlisoy.
But don't just read them - make sure you type the commands in MATLAB and always make your own comments explaining what each line means and does.
In this course we don't go over the installation steps so make sure you have MATLAB downloaded on your device before listening to these lessons.
We also recommend typing all the commands that you see on the lecture slides in MATLAB and you can find the written versions of the slides in PDF in the resources.
Each section has a quiz to help you assess how well you understood the material covered in the lessons.
In our first lesson, you’ll learn what the MATLAB environment looks like. By the end of this lesson, you should know what are working directory, command window, current folder and workspace.
Working directory - the address bar at the top. It's the first place where MATLAB will look and save stuff by default.
Command window - the main window with a promt >> where you type your instructions to Matlab. When you hit return, MATLAB will run these commands.
Current Folder - panel where you can access your files.
Workspace - the location where MATLAB stores and accesses data. Whenever you run a command that returns an output, this information is found in the workspace.
It's important to know common terms that are used when coding in MATLAB as well as understand what certain punctuation marks do. Let's go over some of them:
Variable - a name that we assign to a specific value to avoid repeating the same value in our code.
Scalar - used to present numerical values that are simple numbers.
Vector - a name for a row or column of numbers.
[ ] - used when you want to put more than one number in a variable in MATLAB.
; - used when you want to make a column of numbers.
Matrix - a two-dimensional, rectangular array of data elements arranged in rows and columns.
Element - a single number within a matrix or vector.
Array - a collection of elements with the same data type.
( ) - used to get things out of a matrix or to refer to just a part of a matrix.
: - used when you want to extract data from a matrix; (:) used to rearrange values into a column; used to return numbers in increments.
' - used to transpose a matrix or a vector.
You can also check out and download a document by Mathworks with basic functions and their definitions. Keep it handy when doing the exercises and writing your code.
Indexing is selecting a subset of elements from the matrix, in other words, it let's you get the part of a matrix that you want.
How to do it?
The easiest way to index something is to type the name of the variable you want to take something from and then specify the position of the element you want to index in round brackets. You can also use indexing to put new values into your variables.
When indexing a matrix, you can use two values, the first to specify the row and the second to specify the column.
In MATLAB, you can do basic maths on numbers or on variables. So your variables will be treated just like the numbers they represent.
Most symbols are what you would imagine them to be. Keep in mind that in MATLAB we use a star * to multiply things and a slash / to divide thighs - but an important thing to note is that a star (*) symbol on its own means matrix multiplication and a slash (/) on its own means matrix division.
Now, remember that in MATLAB we can work with both scalars and matrices and if you want to do a scalar multiplication or division, then we need to put a dot next to a star or slash.
You can also do sums between a scalar and a matrix. The sum will be applied to every value of the matrix.
Another way of doing basic maths in MATLAB is to use functions and we will talk about basic functions in the next lesson.
MATLAB has a large number of built-in functions that can help you perform maths or statistics and can also generate matrices quickly and easily.
Essentially, a function is a set of commands that get some inputs and give some outputs. Using the name of a function in a command window lets you run all the code within that function.
For the function to work, you need to put your inputs otherwise known as arguments in round brackets and after the function name.
All functions written by MATLAB have help files to tell you how to use them. To get the help files, you need to type help followed by the command name.
Logical operators are used to decide which part of a code to run next or which elements of a matrix to use in a calculation.
These are the main logical operators and their signs:
> greater than
< lesser than
== is equal to
~= is not equal to
~ not
=< less than or equal to
=> greater than or equal to
& and
| or
When you give MATLAB two values with one of the logical operators in the middle, it can tell you if a statement is true or false using the numbers 1 and 0. False is represented by 0 and true by the number 1.
This kind of response is called "Boolean logic".
Any MATLAB command you write into the command window can also be written into a script.
A script is a list of commands executed one after another in the order they appear on the screen.
You can easily run your script just by typing the name of the script file in MATLAB command window.
To make a script, type the MATLAB function edit and hit return or click on the ‘new script’ icon in the top left corner.
Pixels and Images: A pixel is a unit (typically square) of digital information. The pixels are laid out in a grid, which is an image. In Matlab, an image is a matrix. You can think of each element of a matrix as a pixel.
Bit: a bit is a digit in the binary number system. It can be 0 or 1. 8 bits = 1 byte.
Pixel depth: a bit is a unit of color information in a pixel.
Buffer and ‘Flip’: A buffer is temporary storage. In the present context, what you will do is
prepare what you want to display on the on screen window in a back buffer (off screen) and
then copy the contents to the on screen. You can create as many off screen windows as you want.
Screen: add loaded function that will be used in almost every experimental program with Psychtoolbox.
To get help, type: Screen OpenWindow?
In this Lecture, you will learn how to draw graphs using Matlab.
We will analyse a set of data and show you step-by-step how to use simple lines of code to draw any graph.
We will go through different types of graphs, show you how to change colours and edit your figure.
In this lecture, we will cover main tools and functions.
You will learn when you to use them and how to incorporate into your script these functions:
if
switch
while
for
You will be given an example of these functions in a code to see what they do.
1. If - very useful command and the most basic element. If the logical statement you have is true, the next line of the code will be executed. If it is not true, it will move into the else line, which will have a different outcome.
2. For - if you want to repeat a cycle and run the same script, use for. It will execute the set of commands several times. You may write how many counters you want to have
>> for i=1:3
A(i)=i*2
End
A= 2
A= 4
A= 6
3. Switch – this command will allow you to check which situation is correct. You may have several possibilities (cases) and the script to run associated with that case. If none of the logical alternatives is logical, there is a line otherwise that will execute a code you write.
For example, >> A=5
switch(A)
case 1
disp(‘A is a number’)
case 3
disp(‘A is letter’)
case 5
disp(‘A is five’)
otherwise
disp(‘A is not one or three or five’)
end
‘A is three’
4. While is similar to for loop, however, it will repeat the same loop several times while the statement is correct. Let’s write a code with values for y while x value is 2
>> x = 2
Y = -6
While (x==2_
y=y+1
if(y>1)
x=2
end
end
y=…%run this code and modify the values, see what happens
5. Try…Catch – although it is not a code, you may use that to catch the error and prevent your computer from crashing.
In the next step, you will learn how to create your own function and add it to your script:
To write a function, start with this and follow the same format
>>[outputs] = function_name(inputs)
If you want to check, from where your function is taken, in other words, you want to check which path it is, type
>> path
Then select the Set path on the file menu. If you are not sure which command is chosen to execute, type
>> which
In this lesson, you will learn how to code an if loop to check whether a variable meets logical conditions. One example will involve numbers, colours (string variables) and a logical operation ''for''.
Well done, guys! You have now completed the Beginner MATLAB course! You can download the lecture slides attached in the resources.
Hope you liked it, please leave us a review!
This course covers the basics of programming in MATLAB. You'll get familiar with the MATLAB setup and work environment, you'll understand how to use variables, functions, logical operators and other functions to write your own scripts.
You'll also learn how to load, store, import and analyse data in MATLAB and visualise it (graphs, tables etc) so these are ready for you to use for your dissertation, posters, presentations, research papers etc.
You will learn fundamental computer programming concepts such as variables, flow control statements, logical operators, functions and many others.
You will learn about various data types and how to handle them in MATLAB.
You will learn how to work with matrices.
You will learn about uploading and saving data.
Who should take this course?
- Current neuroscience or psychology students struggling with programming classes.
- Graduate students who want to work in a brain sciences lab and have zero coding experience.
- Anyone who wants to study a degree in neuroscience or psychology at a graduate level.
- Anyone who is interested in learning the basics of coding in MATLAB.
Who are your instructors?
We’re Adriana and Ana, Master's in Neuroscience graduates from City, University of London, UK.
We successfully submitted our final-year dissertations, finished our internships in the decision-making and visual labs and currently work in mental health and digital marketing.
When we started our Master’s degree in Clinical, Social and Cognitive Neuroscience, we had no idea that programming is essential in neuroscience research.
We had zero coding experience and it was so hard to keep up with the pace of in-class coding lessons.
So after reading textbook after textbook, watching all YouTube videos out there and asking for help from experienced programmers, we finally learned how to code in MATLAB for our research projects and want you to learn to do the same!