Matlab:改进树遍历代码 [英] Matlab: Improving a tree traversal code

查看:576
本文介绍了Matlab:改进树遍历代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下所示:

我有一个数据库文件夹,其中包含品牌。每个品牌 logo 查询组成。
我想在所有的<$ c中遍历所有文件( file_1 file_n ) $ c>数据库并对它们执行一些操作。

I have a database folder which contains brands. Each brand consists of logo and query. I want to traverse on all the the files (file_1 to file_n) in all the database and perform some operations on them.

我写了这段代码:

d = dir(database);
isub = [d(:).isdir];
brandsFolders = {d(isub).name}';
brandsFolders(ismember(brandsFolders,{'.','..'})) = [];
[numberOfBrands not_used]=size(brandsFolders); %holds the number of the brands
for i=1:numberOfBrands
    temp=strcat(database,  '\');
    currentBrand=strcat(temp, brandsFolders(i));
    d = dir(currentBrand{1,1});
    isub = [d(:).isdir];
    logoAndQuery = {d(isub).name}';
    logoAndQuery(ismember(logoAndQuery,{'.','..'})) = [];
    logo=strcat(currentBrand, '\', logoAndQuery(1));
    files=dir(logo{1,1});
    [numberOfFiles not_used]=size(files);
    for j=1:numberOfFiles
        if strcmp(files(j).name, '..')~=1 && strcmp(files(j).name, '.')~=1
            %operations on each files(j).name
        end
    end
end

代码正常工作,它遍历所需的文件。
然而,代码有点丑陋和混乱。

The code works fine, it traverse on the desired files. However, the code is little bit ugly and confusing.

我想知道我可以用另一种更好的方法吗?

推荐答案

正如你所说,遍历一组目录。但是,imo,有一些事情你可以做的更简单/我会做不同的:

Traversing through a set of directories goes pretty much as you are doing. However imo, there are some things you can do easier / I would do differently:

brandsFolders = dir(database);
brandsFolders( ~[brandsFolders.isdir] | strcmp({brandsFolders.name},'.') | strcmp({brandsFolders.name},'..')) = [];

for ii=1:numel(brandsFolders)
    logoAndQuery  = dir(fullfile(database,brandsFolders(ii).name));
    logoAndQuery( ~[logoAndQuery.isdir] | strcmp({logoAndQuery.name},'.') | strcmp({logoAndQuery.name},'..')) = [];

    logo = fullfile(databasecurrentBrand,brandsFolders(ii).name), logoAndQuery(1).name);
    files = dir(logo);
    files(strcmp({files.name},'.') | strcmp({files.name},'..'))=[];

    for jj=1:numel(files)
        %operations on each files(j).name
    end
end

(这当然只有当你确定 logoAndQuery(1)将会始终是标志目录。)

或者使用子函数进行dir查询:

(This of course only works if you're sure that logoAndQuery(1) will always be the 'logo' directory.)
or alternatively use a subfunction for the dir-querying:

function dirs = getDirs(strPath)
    dirs = dir(strPath);
    dirnames = {dirs.name};
    dirs ( ~[dirs.isdir] | strcmp(dirnames ,'.') | strcmp(dirnames ,'..')) = [];
end

这给你一些较短的代码,并给出以下内容,其中我也假设'logo'目录中没有目录:

which gives you already some shorter code and gives the following, in which I also assume there are no directories in the 'logo' directories:

brandsFolders = getDirs(database);

for ii=1:numel(brandsFolders)
    logoAndQuery  = getDirs(fullfile(database,brandsFolders(ii).name));
    logo = fullfile(databasecurrentBrand,brandsFolders(ii).name), logoAndQuery(1).name);
    files = dir(logo);
    files([files.isdir])=[];

    for jj=1:numel(files)
        %operations on each files(j).name
    end
end

这篇关于Matlab:改进树遍历代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆