0

我在一个名为 3410001ne => 3809962sw 的文件夹中有大约 1500 张图像。我需要对这些文件中的大约 470 个进行子集化,以便使用 Matlab 代码进行处理。下面是我的 for 循环之前的代码部分,它列出了文件夹中的所有文件:

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 指向文件夹中的一系列文件,而不是整个文件夹?提前致谢。

4

2 回答 2

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
于 2012-06-24T18:29:54.070 回答
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
于 2012-06-24T18:26:20.860 回答