MATLAB: Matlab/SPM Batch Script Help

directoryfor loopMATLAB

Hi guys, I'm new to Matlab and SPM and would like some help coming up with or editing a short script to shorten the input field for some fMRI images so that batch processing across multiple subjects could be done easily.
matlabbatch{1}.spm.temporal.st.scans = {
{
'C:\face_rep\rawepi\RawEPI\rsM03953_0005_0006.img,1'
'C:\face_rep\rawepi\RawEPI\rsM03953_0005_0007.img,1'
'C:\face_rep\rawepi\RawEPI\rsM03953_0005_0008.img,1'
'C:\face_rep\rawepi\RawEPI\rsM03953_0005_0009.img,1'
.
.
.
'C:\face_rep\rawepi\RawEPI\rsM03953_0005_0300.img,1'
}
}';
matlabbatch{1}.spm.temporal.st.nslices = 24;
An example I was previously given was the use of a for loop which allows the files for each subject to be processed with a simple change of the directories and works well.
cd(imgdir);
imgfiles = ls('r*.img');
for spmimages = 1:size(imgfiles, 1)
matlabbatch{1}.spm.temporal.st.scans(spmimages,:) = {['C:\face_rep\rawepi\RawEPI\' imgfiles(spmimages,:)]};
end
However, when I try to do up the loop from scratch, I am a little unsure about how to define the 'spmimages' and 'matlabbatch' variables from scratch as I get the "Error using ==> horzcat" message so any help there would be greatly appreciated. Thanks!

Best Answer

cd(imgdir);
imgfiles = dir('r*.img');
for spmimages = 1:size(imgfiles, 1)
matlabbatch{1}.spm.temporal.st.scans{spmimages} = fullfile(imgdir, imgfiles(spmimages).name);
end
Or more simply,
cd(imgdir);
imgfiles = dir('r*.img');
matlabbatch{1}.spm.temporal.st.scans = strcat(imgdir, '\', { imgfiles.name });
The above assume you are just constructing the name, not loading data from them.
Related Question