觉得有帮助?欢迎来打赏
# 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