5
$\begingroup$

I'm aware of the ambivalence of cogsci.stacexchange with regards to asking questions about code, but this question really is about SPM, specifically, so I think that the domain-specific nature of my inquiry makes it suitable for this forum. I do apologize if this is in poor form.

I'm trying to run a custom batch script on some fMRI data. The relevant code is presented below:

cfg = config();  % a struct that holds a few parameters, namely paths and regexps

for isub = 1:length(cfg.subdir)
    subdir = cfg.subdir(isub);
    rundirs = fullfile(subdir, regexpdir(subdir, cfg.regex.rundir));

    matlabbatch{isub}.spm.spatial.realign.estwrite.data = regexpdir(fullfile(subdir), cfg.regex.funcscan);
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.quality = 1.0;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.sep = 4;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.rtm = 2;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.interp = 2;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.weight = '';
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.which = [0 1];  % [0 1] = create mean images only; [1 1] crt all imgs + mean
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.interp = 4;  % 4th degree bspline interp
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.mask = 1;
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
end

This loop, which performs slice realignment for all subjects, is called via spm_jobman('serial', 'myscript.m') where myscript.m is the script shown above.

regexpdir is available on the matlab file exchange for those who wish to examine it, but it returns an Nx1 cell array of strings, which correspond to paths matched by the regex parameter.

Running this code doesn't seem to produce any errors, per se, but rather prints the following several thousand times:

Item 'Session', field 'val': Value must be either empty, a cellstr or a cfg_dep object.

The variable name cfg_dep makes me think of SPM's "dependency" construct, often used in the visual batch tool. The code above is taken from said visual batch tool's code exportation feature, so it's possible that I'm referencing a dependency that somehow got omitted when I refactored the exported code... but damned if I can find where...

Any thoughts or advice would be greatly appreciated!

$\endgroup$
2
$\begingroup$

Below is a corrected version of the script. SPM seems to be quite finicky about the structure of the data passed to matlabbatch{...}.spm.spatial.realign.estwrite.daat{...}.


%%job_realign.m  SPM batch module.
% SPM's spm_jobman function will eval this file, and automatically launch a
% batch job if the parameters are valid.  Cryptic errors out of spm_jobman
% are usually an indication of a runtime error in batch modules.

%% Get configuration

cfg = config();

%% Get data
include = cfg.subdir(find(cfg.submask));

for isub = 1:length(include)
    subdir = include{isub};
    fprintf('In directory %s:\n', fileparts(subdir));

    runs = regexpdir(subdir, cfg.regex.rundir, false)';
    runs = [runs regexpdir(subdir, cfg.regex.locdir, false)'];
    runs = [runs regexpdir(subdir, cfg.regex.retdir, false)'];


    for irun=1:length(runs)
        rundir = runs{irun};
        scans = regexpdir(rundir, cfg.regex.funcscan, false);
        fprintf('Scans:  %d\n', size(scans, 1));

        matlabbatch{isub}.spm.spatial.realign.estwrite.data{irun} = scans;
    end



    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.sep = 4;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.interp = 2;
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
    matlabbatch{isub}.spm.spatial.realign.estwrite.eoptions.weight = {''};
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.which = [0 1];    % [0 1] creates mean image only; [1 1] creates all images + mean
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.interp = 4;       % 4th degree B-Spline interpolation
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.mask = 1;
    matlabbatch{isub}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.