Matlab对文件夹的层次遍历和深度遍历

         最近做一个项目,由于数据分别放在不同的文件夹中,对大量数据文件“打开->复制->粘贴”,觉得很费事,于是就写了对基于Matlab的文件夹遍历。文价夹遍历有两种方式,即层次遍历和深度遍历。个人比较倾向用层次遍历的方法,因为深度遍历要用到递归,当文件目录比较深的时候可能会出现栈溢出的现象(当然这只是个极端的情况),而且必须要做成一个函数,若需要记录每个文件的路径,就比较麻烦!而层次遍历思路相对简单,易于理解,废话不多说,直接贴上代码:

        【由于之前版本中有一些错误,现在修改过来了,并且给出了函数的调用Demo,欢迎大家一起交流学习

1、基于matlab的深度优先遍历:

%   Input:
%    strPath: the directory of the file   
%    mFiles:  save the directory of the files
%    iTotalCount: the count of the walked files
%   Ouput:
%    mResFiles: the full directory of every file   
%    iTCount:   the total file count in the directory which your hava input


function [ mResFiles, iTCount ] = DeepTravel( strPath, mFiles, iTotalCount )
    iTmpCount = iTotalCount;
    path=strPath;
    Files = dir(fullfile( path,'*.*'));
    LengthFiles = length(Files);
    if LengthFiles <= 2
        mResFiles = mFiles;
        iTCount = iTmpCount;
        return;
    end


    for iCount=2:LengthFiles
        if Files(iCount).isdir==1  
            if Files(iCount).name ~='.'  
                filePath = [strPath  Files(iCount).name '/'];  
                [mFiles, iTmpCount] = DeepTravel( filePath, mFiles, iTmpCount);
            end  
        else  
            iTmpCount = iTmpCount + 1;
            filePath = [strPath  Files(iCount).name]; 
            mFiles{iTmpCount} = filePath;
        end 
    end
    mResFiles = mFiles;
    iTCount = iTmpCount;
end

2、基于Matlab的层次遍历(广度优先遍历):

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

3、调用Demo:

clc
clear
close all

%% The directory of your files
str = 'C:/test/';

%% The use of depth-first walk
mFiles = [];
[mFiles, iFilesCount] = DeepTravel(str,mFiles,0)
mFiles = mFiles';

%% The use of breadth first walk
mFiles2 = RangTraversal(str)


 

 

要在Matlab遍历文件夹,可以使用dir函数来获取文件夹中的所有文件和文件夹的信息。可以像这样使用dir函数来遍历某个路径下的所有文件夹和文件: ```matlab path = 'E:/matlabTest'; files = dir(path); for i = 1:length(files) if files(i).isdir && ~strcmp(files(i).name,'.') && ~strcmp(files(i).name,'..') folder_path = fullfile(path, files(i).name); % 对文件夹的处理操作 % 如果需要遍历文件夹中的文件 folder_files = dir(fullfile(folder_path, '*.mat')); for j = 1:length(folder_files) file_path = fullfile(folder_files(j).folder, folder_files(j).name); % 对文件的处理操作 end end end ``` 上述代码会遍历指定路径下的所有文件夹和文件。你可以根据需要在对文件夹和文件进行处理的部分添加自己的代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用MATLAB遍历文件](https://blog.csdn.net/weixin_45404593/article/details/123914077)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【Matlab学习】 遍历文件夹下的所有文件](https://blog.csdn.net/chenriwei2/article/details/42321851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值