matlab遍历文件并完成文件移动

只能遍历一层的code:

path = 'E:\car\20160918MA\data\image\';  
pth = 'E:\car\20160918MA\data\imgtmp\';
fileExt = '*.jpg';  
files = dir(fullfile(path,fileExt));  
len = size(files);  
for i=1:len  
fileName = strcat(path,files(i,1).name)
copyfile(fileName, pth);   
end; 

能遍历多层code:

% RangTraversal script
function [ mFiles ] = RangTraversal( strPath )  
    %定义两数组,分别保存文件和路径  
    mFiles = cell(0,0);  
    mPath  = cell(0,0);  
      
    mPath{1}=strPath;  
    [r,c] = size(mPath);  
    while c ~= 0  
        strPath = mPath{1};  
        Files = dir(fullfile( strPath,'*.*'));  
        LengthFiles = length(Files);  
        if LengthFiles == 0  
            break;  
        end  
        mPath(1)=[];  
        iCount = 1;  
        while LengthFiles>0  
            if Files(iCount).isdir==1  
                if Files(iCount).name ~='.'  
                    filePath = [strPath  Files(iCount).name '/'];  
                    [r,c] = size(mPath);  
                    mPath{c+1}= filePath;  
                end  
            else  
                filePath = [strPath  Files(iCount).name];  
                [row,col] = size(mFiles);  
                mFiles{col+1}=filePath;  
            end  
  
            LengthFiles = LengthFiles-1;  
            iCount = iCount+1;  
        end  
        [r,c] = size(mPath);  
    end  
  
    mFiles = mFiles';  
end 


 %demo

%% The directory of your files  
str = 'E:/car/20160918MA/data/label/';  
pth = 'E:\car\20160918MA\data\mark\';  
%% The use of depth-first walk  
%mFiles = [];  
%[mFiles, iFilesCount] = DeepTravel(str,mFiles,0)  
%mFiles = mFiles';  
  
%% The use of breadth first walk  
mFiles2 = RangTraversal(str)  
len = size(mFiles2);  
for i=1:len  
fileName =mFiles2{i};
copyfile(fileName, pth);   
end; 

参考:http://blog.csdn.net/carson2005/article/details/17263083

            http://blog.csdn.net/guoxiaojie_415/article/details/21317323

            http://blog.csdn.net/stpeace/article/details/8230476

你可能感兴趣的:(matlab)