CAT12 Error using MATLABbatch system

12 views (last 30 days)
Mia DeCataldo
Mia DeCataldo on 24 Sep 2025 at 20:16
Answered: Umar on 25 Sep 2025 at 4:51
Using CAT12 segmentation to get image quality measures on mprage data. I'm doing batches of people. I'm working on Linux. The code works, but produces the following error when running a batch with more than one participant using paralell processing:
Completed volumes (see catlog files for details!):
05-Sep-2025 10:44:44 - Failed
'CAT12: Segmentation'
Array indices must be positive integers or logical values.
In file "/home/mid80/Resources/spm12/toolbox/cat12/cat_run.m" (???), function "cat_run" at line 526.
The following modules did not run:
Failed: CAT12: Segmentation
Error using MATLABbatch system
Job execution failed. The full log of this run can be found in MATLAB command window, starting with the lines (look for the line showing the exact #job as displayed in this error message)
Running job #1
We get the expected results from the code (reports, mri files, etc.), but none of the robust output you're supposed to see while the code is running. The code is meant to print "Done" when finished (the following line after calling spm_jobman() ), but it stops printing any output after the error shows & MATLAB does not read as "Busy", but the job still runs even though it says it there is an error. How can I get it to run without the error? My code below:
% STEP 0: Set your file path
user = "mid80"; % ← CHANGE THIS TO YOUR USERNAME
cd("/home/" + user + "/ProjectDrive/CHARM");
% STEP 1: Set the batch number (edit this before each run)
batch_number = 1; % ← CHANGE THIS TO 2, 3, 4, etc. for each batch; can specify more than one too (e.g., [1:5] for batches 1-5)
% STEP 2: Setup
spm('defaults', 'fmri');
spm_jobman('initcfg');
% STEP 3: Read lookup table (assumes sub_id = CXXXX)
lookup_table = readtable('CAT12/lookup.xls', 'Sheet', 1);
% Filter for the current batch
batch_subjects = lookup_table.sub_id(ismember(lookup_table.batch, batch_number));
% STEP 4: Build full paths to each subject's T1w file
current_files = cell(size(batch_subjects));
for i = 1:length(batch_subjects)
subj_id = batch_subjects{i};
% Build expected filename: sub-CXXXX_ses-01_T1w.nii
search_pattern = fullfile('MPRAGE', sprintf('sub-%s_ses-01_T1w.nii', subj_id));
file_struct = dir(search_pattern);
if isempty(file_struct)
error('T1w file not found for subject %s using pattern %s', subj_id, search_pattern);
elseif length(file_struct) > 1
error('Multiple T1w files found for subject %s', subj_id);
end
current_files{i} = fullfile(file_struct.folder, file_struct.name);
end
fprintf('\n=== Running batch %d: %d subjects ===\n', batch_number, length(current_files));
disp(batch_subjects);
% STEP 5: Load template and inject current subject paths
run('cat12_segmentation_template_move.m'); % loads matlabbatch
disp('loaded matlabbatch');
% Overwrite subject paths
matlabbatch{1}.spm.tools.cat.estwrite.data = current_files;
% Set resources path
matlabbatch{1}.spm.tools.cat.estwrite.opts.tpm = {'/home/' + user + '/Resources/spm12/tpm/TPM.nii'};
matlabbatch{1}.spm.tools.cat.estwrite.extopts.registration.shooting.shootingtpm = {'/home/'+ user + '/Resources/spm12/toolbox/cat12/templates_MNI152NLin2009cAsym/Template_0_GS.nii'};
% Save the updated batch for record-keeping
save(sprintf('cat12_batch_manual_batch%02d.mat', batch_number), 'matlabbatch');
% STEP 6: Run segmentation
spm_jobman('run', matlabbatch); % Error occurs when running this spm_jobman()
fprintf('**************DONE batch %02d *********************', batch_number);

Answers (1)

Umar
Umar on 25 Sep 2025 at 4:51

Hi @Mia DeCataldo ,

Your " Array indices must be positive integers" error at cat_run.m line 526 is a known CAT12 parallel processing conflict that occurs when batch processing multiple subjects simultaneously with your current workflow (loading template via cat12_segmentation_template_move.m then injecting current_files into matlabbatch) - the error happens in CAT12's logging subsystem (which is why MATLAB doesn't show "*Busy*” but jobs continue running and results still generate), not in the core segmentation engine, so your outputs remain valid despite the error message (ref: https://www.mathworks.com/help/spm/ ). The quickest fix is to disable parallel processing by adding spm_get_defaults('cmdline', true); maxNumCompThreads(1); before your spm_jobman('run', matlabbatch);line, or alternatively process subjects sequentially in a loop with individual batches to eliminate the indexing conflicts entirely while maintaining full verbose output and proper "Done" messaging (ref: https://www.mathworks.com/help/parallel-computing/ ). Additionally, replace your string concatenations with fullfile()for the TPM and shooting template paths to ensure cross-platform compatibility (ref: https://www.mathworks.com/help/matlab/ref/fullfile.html ). This is a documented limitation in CAT12's interface with SPM12's batch system on Linux (ref: https://neuro-jena.github.io/cat12/ ), and either solution will resolve the error while preserving all functionality.

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!