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

<hr>

<pre><code>%%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
</code></pre>

CC BY-SA 4.0
2021-11-20T04:20:27.070Z
Thanks this helped me with a similar issues where I created a cell to assign to .data, which even matlab thought is equal to what is created by SPM when I save the batch as a script but I got the same error message. In my case though I found the issue is that my cell contain the data
{["xxxx\sub-01\func\sub-01_task-flanker_run-1_bold.nii,1"  ]}

instead of
{'xxxx\sub-01\func\sub-01_task-flanker_run-1_bold.nii,1'  }. Maybe this knowledge will help anyone.
JAQuent
CC BY-SA 3.0
2015-09-30T15:09:18.980Z
2015-09-30T15:09:18.980Z
Louis Thibault
<p>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.</p>

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

<pre><code>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
</code></pre>

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

<p><code>regexpdir</code> 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.</p>

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

<pre><code>Item 'Session', field 'val': Value must be either empty, a cellstr or a cfg_dep object.
</code></pre>

<p>The variable name <code>cfg_dep</code> 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...</p>

<p>Any thoughts or advice would be greatly appreciated!</p>

<fmri><spm><analysis>
Why is my SPM batch job printing a warning repeatedly?
Louis Thibault
# Why is my SPM batch job printing a warning repeatedly?

(Asked by: username_0 on 2015-09-25)

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!

**Tags:** fmri, spm, analysis


---

## 1 answers

### Answer by username_0 on 2015-09-30. Score: 3 | Accepted answer

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

> **Comments:**
> **username_1**: Thanks this helped me with a similar issues where I created a cell to assign to .data, which even matlab thought is equal to what is created by SPM when I save the batch as a script but I got the same error message. In my case though I found the issue is that my cell contain the data {["xxxx\sub-01\func\sub-01_task-flanker_run-1_bold.nii,1"  ]}  instead of {'xxxx\sub-01\func\sub-01_task-flanker_run-1_bold.nii,1'  }. Maybe this knowledge will help anyone.  

---

