【问题标题】:Subset folder contents Matlab子集文件夹内容 Matlab
【发布时间】:2012-06-23 23:12:21
【问题描述】:

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

【问题讨论】:

  • 更具体地说,这些文件是如何准确命名的?据我所知,两者之间只有 3610032-3609902 = 130 个文件,那么您如何得出 470 个文件?

标签: matlab file-io directory


【解决方案1】:

考虑以下几点:

%# 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

【讨论】:

  • 我刚刚修复了代码以包含而不是排除所需的范围,对不起:)
【解决方案2】:

这样的事情可能会奏效。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多