Hi everyone,
I would like to select images from a number of folders. However, the folders have to be selected in a very specific order. What I have is a folder named 'functional' which contains folder epi1, epi2, epi3, epi4, epi5, and epi6. Each of these epi folders contain images I have to process. The function below will select the folders alphabetically (i.e. epi1 then epi2 then epi3, etc). What I want to achieve is to have the folders selected in the following order: epi4, epi5, epi6, epi1, epi2, epi3. Would anyone be able to help me? Here is the function I currently have:
dire_FIX = '/Data2/Test_subject/'
filter_str = 'f'
function P=fun_load_image_data_path(Q,dire_FIX,filter_str)
n = length(Q); % Number of subjects
P = cell(n,1);
for i = 1:n,
dire=[dire_FIX Q{i} '/nifti/WM/functional/']; % go to the functional folder of each subject
lala=dir(dire); % List content of functional folder
ns = length(lala)-2; % To take out the '.' and '..'
temp_pp=[];
if (strcmp(filter_str,'mean')) % if mean BOLD get from the first session
s = 1;
dire1=[dire lala(s+2).name '/'];
P{i}.meanimg =spm_select('FPList',dire1,strcat('^',filter_str,'.*\.nii$'));
else
add_i=1;
for s=1:ns,
dire1=[dire lala(s+2).name '/'];
temp_img = spm_select('FPList',dire1,strcat('^',filter_str,'.*\.nii$'));
for ss=1:length(temp_img),
P{i}.scans{s}{ss,1} = strcat(temp_img(ss,:),',1');
P{i}.path_all{add_i,1} = strcat(temp_img(ss,:),',1');
add_i = add_i+1;
end
fn_temp = [];
fn_temp = spm_select('FPList', dire1, '^rp.*\.txt$');
P{i}.motion_param_files{s}=fn_temp;
end
end
end

 採用された回答

dpb
dpb 2014 年 1 月 28 日
編集済み: dpb 2014 年 1 月 30 日

0 投票

Simplest for a limited case such as this is to simply enumerate --
ADDENDUM: Include empty directory test/skip and example generalization to order vector from formula
idx=circshift(1:6,[0 -3]);
dire_FIX = '/Data2/Test_subject';
for i=1:length(dirlist)
pth=[fullfile(dire_FIX,sprintf('%d',idx(i)))];
if ~(exist(pth,'dir')==7), continue, end
d=dir([pth '*.img']);
for j=1:length(d)
% process file d(j).name here for i-th subdirectory
NB: buiding the former array dynamically now since there is a computable pattern. Choose whatever method is most appropriate to the manner in which the required order is generated. Two possible ways (of many) now been demonstrated...
%dirlist={'epi4'; 'epi5'; 'epi6'; 'epi1'; 'epi2'; 'epi3'};

3 件のコメント

Thang  Le
Thang Le 2014 年 1 月 29 日
Thank you so much for responding. An issue I did not mention earlier is that in some cases one or more of the epi folders may be missing (subjects did not complete run or runs thrown out due to motion). This requires the script to account for such instances without changing the dirlist or the order. For instance, if epi2 is missing, the order should be epi4, epi5, epi6, epi1, epi3.
dpb
dpb 2014 年 1 月 29 日
編集済み: dpb 2014 年 1 月 29 日
Well, you'll then get a null result for the directory scan for the missing folder(s)--a test for that is trivial enough. Or, use exist first to check before asking for the other way 'round. "Defensive programming"
ADDENDUM:
Answer updated to reflect checking first technique...
Thang  Le
Thang Le 2014 年 1 月 31 日
Thank you so much! This really helped.

サインインしてコメントする。

その他の回答 (0 件)

製品

質問済み:

2014 年 1 月 27 日

コメント済み:

2014 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by