【源码】文件名/文件路径的自然排序函数NATSORTFILES version 1.6.0.0

在这里插入图片描述

函数集NATSORTFILES对文件名或文件路径(1xN char)的单元数组进行排序,同时考虑字符串中的所有数字。

The function NATSORTFILES sorts a cellarray of filenames or filepaths (1xN char), taking into account any numbervalues within the strings.

这被称为自然顺序排序或字母数字排序。

This is known as a natural order sort or analphanumeric sort.

请注意,MATLAB的内置排序函数SORT只对字符代码进行排序(与大多数编程语言中的排序一样)。

Note that MATLAB’s inbuilt SORT functionsorts the character codes only (as does SORT in most programming languages).

NATSORTFILES不是一种简单的自然顺序排序,而是将文件名和文件扩展名分开后进行分类,这意味着NATSORTFILES将短文件名排列在长文件名之前,即字典排序。

NATSORTFILES is not a naive natural-ordersort, but splits and sorts filenames and file extensions separately, whichmeans that NATSORTFILES sorts shorter filenames before longer ones: this isknown as a dictionary sort.

出于同样的原因,在每个路径分隔字符(’’ 或 ‘/’)处分割文件路径,每个目录级别被单独排序。

For the same reason filepaths are split atevery path-separator character (either ‘’ or ‘/’), and each directory level issorted separately.

使用NATSORTROWS函数排序字符串单元阵列的行。

For sorting the rows of a cell array ofstrings use NATSORTROWS.

使用NATSORT排序字符串单元阵列。

For sorting a cell array of strings useNATSORT.

基本用法

默认情况下,NATSORTFILES将连续数字认为是单个整数的一部分,每个数字被认为与一个字母的宽度相同:

By default NATSORTFILES interpretsconsecutive digits as being part of a single integer, each number is consideredto be as wide as one letter:

A = {‘a2.txt’, ‘a10.txt’, ‘a1.txt’};
sort(A)
natsortfiles(A)
ans =
‘a1.txt’ ‘a10.txt’ ‘a2.txt’
ans =
‘a1.txt’ ‘a2.txt’ ‘a10.txt’

排序索引

第二个输出参数是排序索引ndx的数字数组:

The second output argument is a numericarray of the sort indices ndx, such that Y = X(ndx) where Y = natsortfiles(X):

[~,ndx] = natsortfiles(A)
ndx =
3 1 2

调试数组

第三个输出参数是单元阵列的单元格向量,其中每个单元阵列包含单个字符和数字(在转换为数字之后)。

The third output is a cell vector of cellarrays, where each cell array contains individual characters and numbers (afterconverting to numeric).

这对于确认数字已经被正常表达式正确识别是有用的。

This is useful for confirming that thenumbers are being correctly identified by the regular expression.

单元向量的单元格对应于分割后的目录、文件名和文件扩展名。

The cells of the cell vector correspond tothe split directories, filenames, and file extensions.

[,,dbg] = natsortfiles(A);
dbg{:}
ans =
‘a’ [ 2]
‘a’ [10]
‘a’ [ 1]
ans =
‘.’ ‘t’ ‘x’ ‘t’
‘.’ ‘t’ ‘x’ ‘t’
‘.’ ‘t’ ‘x’ ‘t’

DIR与单元阵列举例

使用DIR找出文件夹中的所有文件,并对它们进行正确排序,然后依次循环遍历,举例如下。

One common situation is to use DIR toidentify files in a folder, sort them into the correct order, and then loopover them: below is an example of how to do this.

D = ‘natsortfiles_test’; % directory path
S = dir(fullfile(D,’*.txt’)); % get list of files in directory
N = natsortfiles({S.name}); % sort file names into order
for k = 1:numel(N)
disp(fullfile(D,N{k}))
end
natsortfiles_test\A_1.txt
natsortfiles_test\A_1-new.txt
natsortfiles_test\A_1_new.txt
natsortfiles_test\A_2.txt
natsortfiles_test\A_3.txt
natsortfiles_test\A_10.txt
natsortfiles_test\A_100.txt
natsortfiles_test\A_200.txt

DIR与结构体

需要访问DIR结构体的用户可以使用NATSORTFILE的第二个输出参数,并对该输出结构体进行正确排序:

Users who need to accessthe DIR structure fields can use NATSORTFILE’s second output tosort DIR’s output structure into the correct order:

D = ‘natsortfiles_test’; % directory path
S = dir(fullfile(D,’*.txt’)); % get list of files in directory
[~,ndx] = natsortfiles({S.name}); % indices of correct order
S = S(ndx); % sort structure using indices
for k = 1:numel(S)
fprintf(’%-13s%s\n’,S(k).name,S(k).date)
end
A_1.txt 22-Jul-2017 09:13:23
A_1-new.txt 22-Jul-2017 09:13:23
A_1_new.txt 22-Jul-2017 09:13:23
A_2.txt 22-Jul-2017 09:13:23
A_3.txt 22-Jul-2017 09:13:23
A_10.txt 22-Jul-2017 09:13:23
A_100.txt 22-Jul-2017 09:13:23
A_200.txt 22-Jul-2017 09:13:23

字典排序

B = {‘test_ccc.m’; ‘test-aaa.m’; ‘test.m’; ‘test.bbb.m’};
sort(B) % ‘-’ sorts before ‘.’
natsort(B) % ‘-’ sorts before ‘.’
natsortfiles(B) % correct dictionary sort
ans =
‘test-aaa.m’
‘test.bbb.m’
‘test.m’
‘test_ccc.m’
ans =
‘test-aaa.m’
‘test.bbb.m’
‘test.m’
‘test_ccc.m’
ans =
‘test.m’
‘test-aaa.m’
‘test.bbb.m’
‘test_ccc.m’

关于“文件名”的解释

C = {‘test2.m’; ‘test10-old.m’; ‘test.m’; ‘test10.m’; ‘test1.m’};
sort© % Wrong numeric order.
natsort© % Correct numeric order, but longer before shorter.
natsortfiles© % Correct numeric order and dictionary sort.
ans =
‘test.m’
‘test1.m’
‘test10-old.m’
‘test10.m’
‘test2.m’
ans =
‘test.m’
‘test1.m’
‘test2.m’
‘test10-old.m’
‘test10.m’
ans =
‘test.m’
‘test1.m’
‘test2.m’
‘test10.m’
‘test10-old.m’

关于“文件路径”的解释

D = {‘A2-old\test.m’;‘A10\test.m’;‘A2\test.m’;‘AXarchive.zip’;‘A1\test.m’};
sort(D) % Wrong numeric order, and ‘-’ sorts before ‘’:
natsort(D) % correct numeric order, but longer before shorter.
natsortfiles(D) % correct numeric order and dictionary sort.
ans =
‘A10\test.m’
‘A1\test.m’
‘A2-old\test.m’
‘A2\test.m’
‘AXarchive.zip’
ans =
‘A1\test.m’
‘A2-old\test.m’
‘A2\test.m’
‘A10\test.m’
‘AXarchive.zip’
ans =
‘AXarchive.zip’
‘A1\test.m’
‘A2\test.m’
‘A2-old\test.m’
‘A10\test.m’

MATLAB源码下载地址:

http://page5.dfpan.com/fs/4lcdj2221329b160da1/

更多精彩文章请关注微信号:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
kube-proxy启动时报错"failed to start proxier healthz on 0.0.0.0:10256: listen tcp 0.0.0.0:10256: bind: address already in use"是因为在绑定到0.0.0.0:10256时,该地址已经被其他进程占用了。这个错误通常是由于kube-proxy的健康检查端口被其他进程占用导致的。 在kube-proxy的main函数中,通过调用`http.ListenAndServe`来启动健康检查服务器。在`ProxyServer`的`Run`方法中,如果配置了健康检查端口(`s.Config.HealthzPort > 0`),则会启动一个HTTP服务器来处理健康检查请求。然后,通过调用`http.ListenAndServe`来监听指定的地址和端口。 要解决这个问题,你可以尝试以下几个步骤: 1. 检查是否有其他进程正在使用0.0.0.0:10256端口。你可以使用`netstat -tlnp | grep 10256`命令来查看是否有进程在监听该端口。 2. 如果有其他进程在使用该端口,你可以停止该进程或者选择一个未被占用的端口来配置kube-proxy的健康检查端口。 3. 如果你确定没有其他进程在使用该端口,但仍然出现该错误,请检查是否有防火墙或网络策略阻止了kube-proxy访问该端口。你可以尝试关闭防火墙或者修改网络策略来允许kube-proxy访问该端口。 总之,"failed to start proxier healthz on 0.0.0.0:10256: listen tcp 0.0.0.0:10256: bind: address already in use"错误是由于kube-proxy的健康检查端口被其他进程占用或者受到网络策略限制导致的。你可以通过停止占用该端口的进程或者修改网络策略来解决该问题。 #### 引用[.reference_title] - *1* *2* *3* [ kube-proxy码分析 ](https://blog.csdn.net/weixin_34055787/article/details/89536734)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值