필터 지우기
필터 지우기

Selecting folders in a specific order

조회 수: 7 (최근 30일)
Thang  Le
Thang Le 2014년 1월 27일
댓글: Thang Le 2014년 1월 31일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by