Selecting folders in a specific order

3 views (last 30 days)
Thang  Le
Thang Le on 27 Jan 2014
Commented: Thang Le on 31 Jan 2014
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

Accepted Answer

dpb
dpb on 28 Jan 2014
Edited: dpb on 30 Jan 2014
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 Comments
dpb
dpb on 29 Jan 2014
Edited: dpb on 29 Jan 2014
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 on 31 Jan 2014
Thank you so much! This really helped.

Sign in to comment.

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!