What seems to be the problem with my short SPM12 script - it's supposed to sum two neural maps (.nii)?

97 views Asked by At

I'm not afraid this question might be too obscure, but here it goes!

I have limited experience with scripting, and I wrote this script to sum two neural maps (that contain "c1" or "c2" in their names) with SPM12:

dataPath=fileparts(pwd);
dataPath=fullfile(dataPath,'/MATLAB Drive/Target Files');
filterStrC1='^c1';
filterStrC2='^c2';

fileNameC1=spm_select('FPListRec',dataPath,filterStrC1);
fileNameC2=spm_select('FPListRec',dataPath,filterStrC2);

for iSubj=1:size(fileNameC1,1)
    disp(iSubj);
    [filePath, fileName]=fileparts(fileNameC1(iSubj,:));
    fileName=fileName(3:end);
    
    V0_fileName=[ filePath, filesep, 'sum_c12_' ,fileName, '.nii'];
    tpm(iSubj)=spm_imcalc([fileNameC1(iSubjs,:) ;fileNameC2(iSubj,:)], V0_fileName,'i1+i2');
end

disp('sum done!')

However, for some reason, the for loop keeps getting skipped, and the fileName variables are empty every time I run the script.

Can you spot any obvious mistakes with my script?

2

There are 2 answers

1
alle_meije On BEST ANSWER

I think you're right and that spm_select returns an empty array.

So spm_select is used to select a 'c1' and 'c2' image of one subject? If you know that there's always a 'c2' for every 'c1' then it would be easier just to use ls:

c1files = ls ( [ DataPath '/*/*/c1*.nii' ] ); % depending on tree depth
c1files = textscan ( c1files, '%s' );
c1files = c1files {1};

for i = 1: length ( c1files )
    c1 = c1files { i };
    c2 = strrrep ( c1, 'c1', 'c2' );
    tpm ( i ) = spm_imcalc ( [ c1; c2 ], 'i1+i2' );
end

As you can see, you need to manipulate the output of the 'ls' command a bit (as well as the command itself, depending on how many directories down you are): it is just a matrix of characters, that textscan can turn into a cell array of file names. (it's actually a nested array, that's why the other line is needed).

But then you have a list of 'c1' files from which you can build the 'c2' files. You may want to print its size just in case it's 0. Hopefully the modified call to spm_imcalc still works - the syntax should be ok, not sure what the shape of tpm is.

0
Shaun Ivan On

I managed to fix it by deleting the first four lines and directly substituting the values in the spm_select command. I suspect there was something wrong with the dataPath variable. Also, there's an extra "s" in the line following line, making a new variable iSubjs instead of using iSubj:

    tpm(iSubj)=spm_imcalc([fileNameC1(iSubjs,:) ;fileNameC2(iSubj,:)], V0_fileName,'i1+i2');

Which I deleted, so it became:

    tpm(iSubj)=spm_imcalc([fileNameC1(iSubj,:) ;fileNameC2(iSubj,:)], V0_fileName,'i1+i2');

I'm still not completely sure why that worked, but now it works.