This site is a work in progress.
In order to get started, there are a few things you need to know about - this information will help you make sense of what you see in MATLAB. You don't have to fully understand what everything means - you just need to be aware that these things exist.
A variable is a container that stores information - from individual numbers and letters, to groups of numbers and text - in all sorts of arrangements (row, column, table, multi-dimensional matrix or array etc.). You can create variables by assigning data to a label (the variable's name) using the equals sign '='
What are variables used for?
They are convenient placeholders - you can use the variable names can be used in-place of the actual information
Good if the information is long, hard to type and remember. For example, you can store a long file path in a variable called 'data_folder', and next time you want to use this path, you can just type 'data_folder' instead of the typing out the full path.
data_folder = 'D:\my_project\data\raw_eeg\';
Using variables makes it more convenient to modify your code. Using the same example, if you happen to move your study to a different drive - you can just redefine 'data_folder' once, at the start of the code, instead of multiple times.
Variable Types: Variables have different types or 'classes' and the variable takes on the 'class' of the information that's stored in the variable -
Numerical - contains a number.
Scalar/Integer - A single number - can also be referred to as a 'scalar' and a number without decimals is referred to as an 'integer'
Vectors - a single row or column of numbers (1D Matrix)
Matrix - a table of numbers with multiple rows and columns (2D matrix)
The matrix has to look rectangular - each row/column has to have the same number of elements
Can only contain numbers
You can also have 3D matrices, i refer to the 3rd dimension as sheets or layers - you can even 4D+ but we won't go there.
The class type of number is called 'double'
String or Character array- contains text
Depending on how to store the data, the data-type will be different
variable = 'test' and variable2 = "test" will have different data-types
The class type for the first will be 'char' - which stands for character array, the text will be saved as a 1x4 array of letters
The class type for the second will be stored as a 'string' type - the text will be saved as a single block of text.
The texts are equivalent and it doesn't matter what type it is most of the time - just know that there is a difference.
Cells - it's a table, similar to a matrix, except it can store both numbers and strings
It still needs to have a rectangular shape - each row/column has to have the same number of elements
Logical - contains 'TRUE' or 'FALSE' states, also represented at '1' and '0'
You can ask MATLAB whether a set of conditions is 'TRUE' or 'FALSE' for each element of a particular array - using '==' or the 'strcmpi()' function - and it'll give produce an array (similar to matrices and cells) which will tell you which elements satisfy that criteria.
These logical variables can be used to help your code to make decisions about how to proceed
Logical variables can also be useful for sub-setting your data - selecting a portion of your data based on some criteria
Functions are like mini-programs inside of MATLAB. There are many functions inside MATLAB. Each function has a different name, and each function does a different thing - usually related to the name of the function.
Functions are actually MATLAB scripts which have been pre-programmed to do a specific task - you can inspect the code by typing edit functionname. If you type help functionname - a description will come up tell you what the function does and how to use it - functions can differ quite considerably.
Functions look like words immediately followed by round brackets - mean(), length(), size() are all functions - their task is related to the function name
There are 2 main components to a function: Inputs and Outputs
Inputs are for telling the function (1) which data you want to work on, and (2) define which options/settings/parameters you want the functions to use.
Many but not all functions have options - also note that functions will use default settings if you don't specify which parameters to use. All of this stuff is in the documentation - you should always read-up on how to use each function.
Outputs are what come out function when the job is done.
Example: the mean() function's job is to calculate the average of a set of numbers
test_data = [1, 2, 3; 3, 4, 5]; % this is a 2 x 3 matrix of numbers
mean_of_test_data = mean(test_data,1); % this will calculate the mean over the first dimension and save the output into a variable called 'mean_of_test_data'
'test_data' is the input
'1' is the optional setting - tells the function that you want to average over the first dimension (which is rows)
'mean_of_test_data' is the output
mean_of_test_data = [2, 3, 4]; % the end result is the mean for each column (because we told it to average 'over' rows)
you can change the optional parameter to '2' if you want to average over the second dimension, or 'all' if you want average over all the numbers.
Things to note:
Best way to learn how to use functions is to read the description and try to use them
You only need to remember what a function does on a conceptual level - there's no need to memorise how to use a function - you can just look it up again.
To learn new functions - just google 'MATLAB and function and how to...'
Indexing is a way to select a portion or subset of an existing variable - It allows you to isolate parts of your dataset and do different things to different parts of your data.
For example, we might have a variable containing a list of subject id codes of all the participantsin our experiment.
all_subject_id = {'P001ML29', 'P002MR21', 'P003FR18', 'P004FL25'};
If we want to extract a single subject id code from this list - we can do this by putting the 'position' of the bit that we want.
If we want to extract 'P001ML29' we would type '1' into brackets after the variable. We use curly brackets when working with cells '{}' and round brackets when working with numeric variables'()'
all_subject_id{1}
ans = 'P001ML29'
all_subject_id{3}
ans = 'P003FR18'
If you can use this to extract more than 1 element - for example, we we want to extract the 2nd and 4th participant, we would type:
all_subject_id{2:4}
ans = {'P002MR21', 'P003FR18', 'P004FL25'}
This way of indexing requires that you know exactly what your data contains - and it might not be useful in other circumstances.
Another way of indexing is to ask MATLAB a question about a variable, and use the 'TRUE/FALSE' answers it gives to subset the data.
For example - if we want to extract the data from a certain electrode 'FCz', but we don't know which part of the data corresponds to 'FCz'. You can use the string compare function 'strcmpi()' to ask MATLAB to ask which position in the list of electrode channels matches 'FCz'.
channel2useidx = strcmpi(EEG.chanlocs.labels,'FCz')
Once we have this position, we can extract that part of the EEG data, which should contain data form FCz.
EEG.data(channel2useidx,:,:)
This works because the order of EEG.chanlocs.labels and the first dimension of EEG.data have the same order.
For loops allows you to repeat a section of code multiple times. When you create a loop, you have to create a new variable that will be used as a counter. You can use this counter as a logical index to change what the code within the loop does.
subject_id = {'P001ML29', 'P002MR21', 'P003FR18', 'P004FL25'};
for subi = 1:length(subject_id)
disp(subi);
disp(subject_id{subi});
end
In this example, we've created a row vector of subject_id codes. We've assigned 'subi' to be the counter variable, and this will increase from
If vs. Switch:
Can use 'and' and 'or' operators with If but not Switch
Switches are more organised, simpler if you have more conditions
If better for nesting