Похоже, вы хотите получить все файлы в каталоге, имена которых соответствуют определенному формату, а затем обработать их все автоматически. Это можно сделать с помощью функции DIR , чтобы получить список имен файлов в текущем каталоге, а затем с помощью функции REGEXP , чтобы найти имена файлов, которые соответствуют определенному шаблону. Вот пример:
fileData = dir(); %# Get a structure of data for the files in the
%# current directory
fileNames = {fileData.name}; %# Put the file names in a cell array
index = regexp(fileNames,... %# Match a file name if it begins
'^[A-Za-z]+_type\d+\.tif$'); %# with at least one letter,
%# followed by `_type`, followed
%# by at least one number, and
%# ending with '.tif'
inFiles = fileNames(~cellfun(@isempty,index)); %# Get the names of the matching
%# files in a cell array
Если у вас есть массив файлов в inFiles
, который соответствует желаемому шаблону именования, вы можете просто зациклить файлы и выполнить обработку. Например, ваш код может выглядеть так:
nFiles = numel(inFiles); %# Get the number of input files
for iFile = 1:nFiles %# Loop over the input files
inFile = inFiles{iFile}; %# Get the current input file
inImg = imread(inFile); %# Load the image data
[outImg,someNumber] = process_your_image(inImg); %# Process the image data
outFile = [strtok(inFile,'.') ... %# Remove the '.tif' from the input file,
'_' ... %# append an underscore,
num2str(someNumber) ... %# append the number as a string, and
'.tif']; %# add the `.tif` again
imwrite(outImg,outFile); %# Write the new image data to a file
end
В приведенном выше примере используются функции NUMEL , STRTOK , NUM2STR , IMREAD и IMWRITE .