matlab dir 排序,matlab对文件目录进行自然排序

觉得有帮助?欢迎来打赏

L3Byb3h5L2h0dHBzL29vby4wbzAub29vLzIwMTcvMDYvMjIvNTk0YmQ5Njg2MWFlMy5wbmc=.jpg

# matlab对文件目录进行自然排序

[原博客阅读效果更佳。](http://www.cnblogs.com/liutongqing/p/7072878.html)

比如我新建一个tmp文件夹,在该文件夹下新建以下txt文件进行测试

```

a1.txt

a2.txt

a3.txt

a11.txt

a12.txt

b10.txt

```

返回到tmp的上一层文件夹,编写代码,查看该文件夹下的所有文件。

```matlab

clear;clc;close all

d = dir('tmp');

for i = 3:length(d)

disp(d(i).name)

end

```

MATLAB返回的结果是

```

a1.txt

a11.txt

a12.txt

a2.txt

a3.txt

b10.txt

```

WTF,什么鬼,有些时候我们不希望得到这样的结果,吓得我赶紧百度了一下

http://blog.csdn.net/g0m3e/article/details/52982737

看到这个人的博客,实现了我想要的结果,但是链接挂掉了。

换个地,找到了解决办法。

新建函数sort_nat.m

function [cs,index] = sort_nat(c,mode)

%sort_nat: Natural order sort of cell array of strings.

% usage: [S,INDEX] = sort_nat(C)

%

% where,

% C is a cell array (vector) of strings to be sorted.

% S is C, sorted in natural order.

% INDEX is the sort order such that S = C(INDEX);

%

% Natural order sorting sorts strings containing digits in a way such that

% the numerical value of the digits is taken into account. I

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值