2012-06-23 45 views
0

我在一個名爲3410001ne => 3809962sw的文件夾中有大約1500個圖像。我需要將這些文件中的大約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),然而,並不是所有的方向與各關聯根。如何將文件夾內容分組爲包含1500個文件中的470個,範圍從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 
相關問題