%Run this file in matlab from the directory you want the contrast images copied into
%the con* images will be copied over and named subnumber_firstlevelanalysisdirectoryname_contrastname

subjroot = '/imaging/jessica/SART/'; % The root directory of your study
subjects = {'CBU080268',...
'CBU080269',...
'CBU080288',...
'CBU080289',...
'CBU080290',...
'CBU080291',...
'CBU080293',...
'CBU080294',...
'CBU080295',...
'CBU080296',...
'CBU080297',...
'CBU080298',...
'CBU080306',...
'CBU080308',...
'CBU080351'}; %These are the names of the folders for each subject  
sdir_roots = { 'SART_ana_SPM5'}; %The first level analysis directories where the contrast images are
for t = 1:length(sdir_roots)
    sdir_root = sdir_roots{t};
    exts = {'img', 'hdr'}; %copy the contrast header and image files
    con_names = {'cue-rest',...  %All the names of your contrasts
        'target-rest',...
        'uncued - rest',...
        'cued - uncued',...
        'all correct - rest',...
        'all com error - rest'};

    for j = 1:length(con_names)
        con_name = con_names{j}
        fcon_name = con_name(con_name ~= ' ');
        for s = 1:length(subjects)
            dir_name = fullfile(subjroot, subjects{s}, sdir_root);
            S = load(fullfile(dir_name, 'SPM.mat'));
            cons = {S.SPM.xCon(:).name};
            cs = strmatch(con_name, cons, 'exact');
            fprintf('Subject %d, contrast %d\n', s, cs(1));
            for e = 1:length(exts)
                str = sprintf('cp %s%ccon_%.4d.%s %s_%s.%s', ...
                    dir_name, filesep, cs(1), exts{e}, ...
                    subjects{s}, [sdir_root, fcon_name], exts{e});
                unix(str);
            end
        end
    end
end