没有“."的 MATLAB 目录和 '..' [英] MATLAB dir without '.' and '..'

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

问题描述

函数 dir 返回一个类似

的数组<预><代码>...文件夹 1文件夹2

并且每次我必须使用以下方法摆脱前 2 个项目时:

for i=1:numel(folders)文件夹名称 = 文件夹(i).名称;如果文件夹名(1) == '.'% 没做什么继续;结尾do_something(文件夹名)结尾

并且使用嵌套循环可能会导致大量重复代码.

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

感谢您的帮助!

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

for i=3:numel(folders)do_something(文件夹(i).name)结尾

简单地忽略前两项.

但是,请注意@Jubobs 的回答.小心以令人讨厌的字符开头的文件夹名称,这些字符的 ASCII 值小于 ..那么第二种方法就会失败.此外,如果它以 . 开头,则第一种方法将失败:)

因此,请确保您有好的文件夹名称并使用我的简单解决方案之一,或者使用@Jubobs 的解决方案来确保.

解决方案

TL;博士

滚动到我的答案底部,该函数列出了除 ... 之外的目录内容.

详细解答

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

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

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

<预><代码>.├── !你好└── '世界

然后你得到这个

<代码>>>f = 目录;>>对于 k = 1 : 长度(f), disp(f(k).name), end!你好'世界...

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

我建议您参考此 ASCII 表,以获取 ASCII 代码点更小的可见字符的详尽列表而不是句号;请注意,并非所有这些字符都必须是合法的文件名字符.

不列出...

的自定义dir函数

在调用 dir 之后,你总是可以在操作它之前从结构数组中删除两个有问题的条目.此外,为了方便起见,如果您想节省一些精神开销,您可以随时编写一个自定义的 dir 函数来执行您想要的操作:

函数列表 = dir2(varargin)如果 nargin == 0名称 = '.';elseif nargin == 1名称 = 可变参数{1};别的error('输入参数过多.')结尾列表 = 目录(名称);指数 = [];n = 0;k = 1;当 n <2 &&k <= 长度(列表)如果有的话(strcmp(listing(k).name, {'.', '..'}))inds(end + 1) = k;n = n + 1;结尾k = k + 1;结尾列表(索引)= [];

测试

假设目录结构与之前相同,您将得到以下内容:

<代码>>>f = dir2;>>对于 k = 1 : 长度(f), disp(f(k).name), end!你好'世界

the function dir returns an array like

.
..
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?

Thanks for any help!

Edit:

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

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

simply disregarding the first two items.

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 :)

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

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

Detailed answer

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.

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.

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

.
├── !hello
└── 'world

then you get this

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

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).

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.

A custom dir function that does not list . and ..

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天全站免登陆