確定這裏是一個簡單的代碼產生你想要的名稱。爲了簡化,我創建了2個數組,每個數組包含我們想要放入文件名的數字。然後很容易遍歷數組並獲取正確的數字。這是很容易實現你的應用程序:
clear
clc
%// In my example I generate only 8 names
NumSamples = 4;
CheckSample = kron(1:NumSamples,ones(1,2));
OneOrTwo = repmat([1 2],1,NumSamples);
CheckSample和OneOrTwo是2個陣列我指的是。您可以看到對應關係以創建具有適當數字的文件名。
CheckSample =
1 1 2 2 3 3 4 4
OneOrTwo =
1 2 1 2 1 2 1 2
%// For demonstration purposes, create a cell array containing the filenames.
NameCell = cell(1,NumSamples*2);
%/ Now loop through each image (in your case) to generate the file name.
for k = 1:2*NumSamples
NameCell{k} = sprintf('Sample%d_%d',CheckSample(k),OneOrTwo(k));
end
NameCell
NameCell看起來是這樣的:
NameCell =
Columns 1 through 4
'Sample1_1' 'Sample1_2' 'Sample2_1' 'Sample2_2'
Columns 5 through 8
'Sample3_1' 'Sample3_2' 'Sample4_1' 'Sample4_2'
從你上面的代碼,你可以做這樣的事情:
files = dir('*.NEF');
NumSamples = length(files)/2;
CheckSample = kron(1:NumSamples,ones(1,2));
OneOrTwo = repmat([1 2],1,NumSamples);
for k = 1:2*NumSamples
filename = files(k).name;
I = imread(filename);
CurrentName = sprintf('Sample%d_%d',CheckSample(k),OneOrTwo(k));
CurrentName = strcat(CurrentName,'.NEF'); %// Keep right file extension.
imwrite(I,CurrentName);
end
什麼不起作用?你會得到什麼樣的輸出? – 2014-12-07 18:07:15
重新命名一個樣本代碼後停止代碼 – Heyyyy 2014-12-07 18:16:31
ok。出於好奇,什麼是name1(文件)? – 2014-12-07 18:18:46