2012-06-23 2 views
0

У меня около 1500 изображений в папке с именем 3410001ne => 3809962sw. Мне нужно подмножество около 470 из этих файлов для обработки с помощью кода Matlab. Ниже приведен фрагмент кода до моего цикла, который перечисляет все файлы в папке:Содержимое папки подмножества Matlab

workingdir = 'Z:\project\code\'; 
datadir = 'Z:\project\input\area1\';  
outputdir = 'Z:\project\output\area1\'; 

cd(workingdir) %points matlab to directory containing code 

files = dir(fullfile(datadir, '*.tif')) 
fileIndex = find(~[files.isdir]); 
for i = 1:length(fileIndex) 
    fileName = files(fileIndex(i)).name; 

Файлов также порядковые направления присоединенных (например 3410001ne, 3410001nw), однако, не все направления, связанные с каждым корень. Как я могу подмножить содержимое папки, чтобы включить 470 из 1500 файлов в диапазоне от 3609902sw => 3610032sw? Есть ли команда, в которой вы можете указать Matlab на ряд файлов в папке, а не на всю папку? Заранее спасибо.

+0

быть более точным, то как эти файлы с именем точно? Насколько я могу судить, между ними всего 3610032-3609902 = 130 файлов, так как у вас есть 470? – Amro

ответ

3

Рассмотрим следующий пример:

%# generate all possible file names you want to include 
ordinalDirections = {'n','s','e','w','ne','se','sw','nw'}; 
includeRange = 3609902:3610032; 
s = cellfun(@(d) cellstr(num2str(includeRange(:),['%d' d])), ... 
    ordinalDirections, 'UniformOutput',false); 
s = sort(vertcat(s{:})); 

%# get image filenames from directory 
files = dir(fullfile(datadir, '*.tif')); 
files = {files.name}; 

%# keep only subset of the files matching the above 
files = files(ismember(files,s)); 

%# process selected files 
for i=1:numel(files) 
    fname = fullfile(datadir,files{i}); 
    img = imread(fname); 
end 
+1

Я просто исправил код, чтобы включить, а не исключать желаемый диапазон, извините :) – Amro

1

Возможно, что-то подобное может работать.

list = dir(datadir,'*.tif'); %get list of files 
fileNames = {list.name}; % Make cell array with file names 
%Make cell array with the range of wanted files. 
wantedNames = arrayfun(@num2str,3609902:3610032,'uniformoutput',0); 

%Loop through the list with filenames and compare to wantedNames. 

for i=1:length(fileNames) 
% Each row in idx will be as long as wantedNames. The cells will be empty if 
% fileNames{i} is unmatched and 1 if match. 
    idx(i,:) = regexp(fileNames{i},wantedNames); 
end 
idx = ~(cellfun('isempty',idx)); %look for unempty cells. 
idx = logical(sum(,2)); %Sum each row 
Смежные вопросы