不带'.'的MATLAB目录和 '..' [英] MATLAB dir without '.' and '..'

查看:107
本文介绍了不带'.'的MATLAB目录和 '..'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数dir返回类似

.
..
Folder1
Folder2

并且每次我必须摆脱前两项时,都需要使用类似的方法:

and every time I have to get rid of the first 2 items, with methods like :

for i=1:numel(folders)
    foldername = folders(i).name;
    if foldername(1) == '.' % do nothing
            continue;
    end
    do_something(foldername)
end

并带有嵌套循环,可能导致大量重复代码.

and with nested loops it can result in a lot of repeated code.

那么我可以通过一种更简单的方法来避免这些文件夹"吗?

So can I avoid these "folders" by an easier way?

感谢您的帮助!

最近我一直在更简单地处理这个问题,就像这样:

Lately I have been dealing with this issue more simply, like this :

for i=3:numel(folders)
    do_something(folders(i).name)
end

只需忽略前两个项目.

,请注意@Jubobs的回答.请注意以讨厌字符开头的文件夹名称,其ASCII值小于..然后,第二种方法将失败.另外,如果以.开头,则第一种方法将失败:)

BUT, pay attention to @Jubobs' answer. Be careful for folder names that start with a nasty character that have a smaller ASCII value than .. Then the second method will fail. Also, if it starts with a ., then the first method will fail :)

因此,请确保您拥有漂亮的文件夹名称并使用我的简单解决方案之一,或者使用@Jubobs的解决方案进行确认.

So either make sure you have nice folder names and use one of my simple solutions, or use @Jubobs' solution to make sure.

推荐答案

TL; DR

滚动到我的答案的底部,找到列出除...之外的目录内容的功能.

TL; DR

Scroll to the bottom of my answer for a function that lists directory contents except . and ...

...条目分别对应于当前文件夹和父文件夹.在* nix shell中,可以使用ls -lA之类的命令列出除...之外的所有内容.可悲的是,MATLAB的dir没有提供此功能.

The . and .. entries correspond to the current folder and the parent folder, respectively. In *nix shells, you can use commands like ls -lA to list everything but . and ... Sadly, MATLAB's dir doesn't offer this functionality.

但是,一切并没有丢失.由dir函数返回的输出struct数组的元素实际上是根据name字段按字典顺序排序的.这意味着,如果您当前的MATLAB文件夹中包含的文件/文件夹以小于句号的任何ASCII码点的字符开头(十进制为46),则...将会不是对应于该struct数组的前两个元素.

However, all is not lost. The elements of the output struct array returned by the dir function are actually ordered in lexicographical order based on the name field. This means that, if your current MATLAB folder contains files/folders that start by any character of ASCII code point smaller than that of the full stop (46, in decimal), then . and .. willl not correspond to the first two elements of that struct array.

这是一个说明性示例:如果您当前的MATLAB文件夹具有以下结构(!hello'world是文件或文件夹),

Here is an illustrative example: if your current MATLAB folder has the following structure (!hello and 'world being either files or folders),

.
├── !hello
└── 'world

那么你得到这个

>> f = dir;
>> for k = 1 : length(f), disp(f(k).name), end
!hello
'world
.
..

为什么...不是前两个条目,在这里?因为感叹号和单引号的代码点(分别为十进制的33和39)比句号(十进制为46)小.

Why are . and .. not the first two entries, here? Because both the exclamation point and the single quote have smaller code points (33 and 39, in decimal, resp.) than that of the full stop (46, in decimal).

我请您参考 ASCII表,以获得ASCII码点较小的可见字符的详尽列表.而不是句号请注意,尽管并非全部都是合法的文件名字符.

I refer you to this ASCII table for an exhaustive list of the visible characters that have an ASCII code point smaller than that of the full stop; note that not all of them are necessarily legal filename characters, though.

在调用dir之后,您始终可以在操作它之前摆脱struct数组中的两个有问题的条目.此外,为方便起见,如果您想节省一些精神上的负担,可以随时编写一个自定义的dir函数来实现您想要的功能:

Right after invoking dir, you can always get rid of the two offending entries from the struct array before manipulating it. Moreover, for convenience, if you want to save yourself some mental overhead, you can always write a custom dir function that does what you want:

function listing = dir2(varargin)

if nargin == 0
    name = '.';
elseif nargin == 1
    name = varargin{1};
else
    error('Too many input arguments.')
end

listing = dir(name);

inds = [];
n    = 0;
k    = 1;

while n < 2 && k <= length(listing)
    if any(strcmp(listing(k).name, {'.', '..'}))
        inds(end + 1) = k;
        n = n + 1;
    end
    k = k + 1;
end

listing(inds) = [];

测试

假设目录结构与以前相同,您将获得以下信息:

Test

Assuming the same directory structure as before, you get the following:

>> f = dir2;
>> for k = 1 : length(f), disp(f(k).name), end
!hello
'world

这篇关于不带'.'的MATLAB目录和 '..'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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