dafni

Brain images + timecourses in Matlab

The aims of this lesson are:

Matlab, two ways…

Before we dig in

makeMontage()

For the first part of the session, I will do a coding walk-through with some narrated advice on problem solving, etc. In the second part of the session, you will then apply the ideas from the first part of make your own function for extracting timecourses from data.

example of a montage

If you want, you can also have a more detailed look at the files and exercises in the matlab_images folder.

returnTimecourse()

In the second part of the session, we want you to make use of the ideas we introduced in the first part to:

This would give you a function signature like this:

function tcourse = returnTimecourse(data, xcoord, ycoord, zcoord)

% code

end

You can then reuse this code/function elsewhere:

% SPM data...
% identify fMRI scan and interesting coordinates.. 
% use it like this...

t = returnTimecourse(data, 30, 5, 2);
plot(t, 'linewidth',2)
xlabel('Time'); 
ylabel('fMRI response')
FSL/FEAT equivalent

If you had data analysed in a FEAT session, you could look at the pre-processed data like this:

% so you can use it with cd('fmri.feat') % go into the folder data = niftiread('filtered_func_data.nii.gz');


correct version

Applying this for spm data

plot() throws errors

Follow-up, solutions (a bit more advanced)

% load 4d data
data = niftiread('filtered_func_data.nii.gz');
% reshape ...
sz = size(data);
sz_space = sz(1)*sz(2)*sz(3)
sz_time = sz(4) 
data_snake = reshape(data,[sz_space sz_time]);
size(data_snake)
x = [30,31,32]; 
y = [5,5,5]; 
z = [2,2,2];
t = [1, 1, 1];
linear_idx = sub2ind(size(data),x,y,z,t);

% then
tcourses = data_snake(linear_idx,:);
% make sure they are columns
tcourses = tcourses';

figure()
plot(tcourses);