In the last tutorial we write a bunch of code to compute confidence intervals. However, this seemed like a lot of work. If we want to do something like this repetitively, it makes a lot more sense to have a function, or script, that does just that. The difference between a function and a script is that a function typically returns something such a a value or values. Here is the code for a simple function. NOTE, we will want to save this function in our MATLAB Tutorial directory as we will use it quite a bit from here on out.
function [CIs] = makeCIs(data)
data_size = length(data);
df = data_size - 1;
t_critical = tinv(0.025,df);
std_data = std(data);
sqrtn = sqrt(data_size);
CIrange = abs(t_critical*std_data/sqrtn);
CIs(1) = mean(data) - CIrange;
CIs(2) = mean(data) + CIrange;
end
This code is also HERE which you can simply download to your Tutorial directory. Now the advantage to making this function is simple - you can make confidence intervals any time you want by simply typing at the Command Prompt or in a script:
newCIs = makeCIs(my_data.RT(my_data.Condition == 1))
So now you have this for life! We are going to continue to build scripts like this so you can will have one that loads your data, plots your data, does some statistics, and provides results.
Functions are very powerful and we will use them as much as we can from here on out. Move onto the next tutorial now.
function [CIs] = makeCIs(data)
data_size = length(data);
df = data_size - 1;
t_critical = tinv(0.025,df);
std_data = std(data);
sqrtn = sqrt(data_size);
CIrange = abs(t_critical*std_data/sqrtn);
CIs(1) = mean(data) - CIrange;
CIs(2) = mean(data) + CIrange;
end
This code is also HERE which you can simply download to your Tutorial directory. Now the advantage to making this function is simple - you can make confidence intervals any time you want by simply typing at the Command Prompt or in a script:
newCIs = makeCIs(my_data.RT(my_data.Condition == 1))
So now you have this for life! We are going to continue to build scripts like this so you can will have one that loads your data, plots your data, does some statistics, and provides results.
Functions are very powerful and we will use them as much as we can from here on out. Move onto the next tutorial now.