1. 程式人生 > >Matlab 中的copyfile函式使用小記

Matlab 中的copyfile函式使用小記

       因為最近使用labelImg軟體標註訓練圖片,我把標記好的圖片和標註檔案放置在一個資料夾下,由於有多批次圖片標註,每標註一批放在一個資料夾下,最終放置的資料夾如下:

現在我需要把這些資料夾下的圖片和標註檔案集中到兩個檔案ImSet(用於放置所有的標註圖片),AnotSet(用於放置所有的標註檔案)

為此我寫了一個Matlab程式,主要函式是copyfile,好了現在直接上Matlab程式吧

%2018/09/07 by DQ
clc;
clear;
close all;
DisperalMainFolder='H:\WorkFile\SecondPeroidAnotBackup';
ConcentrateFolder='C:\Users\Administrator\Desktop\SecondPeroidUnifySet';
OldFileSet=dir(ConcentrateFolder);
OldFileSet(1:2)=[];
if ~isempty(OldFileSet)
   disp('the folder exists other files');
   return
end
ImFolderPath=fullfile(ConcentrateFolder,'ImSet');
if ~exist(ImFolderPath,'dir')
    mkdir(ImFolderPath);
end
AnotFolderPath=fullfile(ConcentrateFolder,'AnotSet');
if ~exist(AnotFolderPath,'dir')
    mkdir(AnotFolderPath);
end

FolderSet=dir(DisperalMainFolder);
FolderNum=length(FolderSet);
for i=3:FolderNum
    FolderName=FolderSet(i).name;
    FolderPath=fullfile(DisperalMainFolder,FolderName);
    XmlFileSet=dir(strcat(FolderPath,'\*.xml'));
    XmlFileNum=length(XmlFileSet);
    fprintf('%s XmlFileNum=%d\n',FolderName,XmlFileNum);
    %%%%%%%start%%%%%%%%%%
    for k=1:XmlFileNum
        XmlFileName=XmlFileSet(k).name;
        XmlFilePath=fullfile(FolderPath,XmlFileName);
        copyfile(XmlFilePath,AnotFolderPath);%複製標註檔案到指定的資料夾
        ImName=strcat(XmlFileName(1:end-4),'.jpg');
        ImPath=fullfile(FolderPath,ImName);
        copyfile(ImPath,ImFolderPath);%複製圖片到指定的資料夾
    end
    %%%%%%%end%%%%%%%%%%
%     %%上述start和end之間的程式有一種更簡潔的方式
%     XmlFileS=strcat(FolderPath,'\*.xml');%複製資料夾FolderPath下所有後綴名為.xml的檔案
%     copyfile(XmlFileS,AnotFolderPath);
%     ImFileS=strcat(FolderPath,'\*.jpg');%複製資料夾FolderPath下所有後綴名為.jpg的檔案
%     copyfile(ImFileS,ImFolderPath);
end