作者:tongqingliu

转载请注明出处:http://www.cnblogs.com/liutongqing/p/7072878.html

觉得有帮助?欢迎来打赏


# 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. It is
% especially useful for sorting file names containing index numbers with
% different numbers of digits. Often, people will use leading zeros to get
% the right sort order, but with this function you don't have to do that.
% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort
% will give you
%
% {'file1.txt' 'file10.txt' 'file2.txt'}
%
% whereas, sort_nat will give you
%
% {'file1.txt' 'file2.txt' 'file10.txt'}
%
% See also: sort % Version: 1.4, 22 January 2011
% Author: Douglas M. Schwarz
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'}) % Set default value for mode if necessary.
if nargin < 2
mode = 'ascend';
end % Make sure mode is either 'ascend' or 'descend'.
modes = strcmpi(mode,{'ascend','descend'});
is_descend = modes(2);
if ~any(modes)
error('sort_nat:sortDirection',...
'sorting direction must be ''ascend'' or ''descend''.')
end % Replace runs of digits with '0'.
c2 = regexprep(c,'\d+','0'); % Compute char version of c2 and locations of zeros.
s1 = char(c2);
z = s1 == '0'; % Extract the runs of digits and their start and end indices.
[digruns,first,last] = regexp(c,'\d+','match','start','end'); % Create matrix of numerical values of runs of digits and a matrix of the
% number of digits in each run.
num_str = length(c);
max_len = size(s1,2);
num_val = NaN(num_str,max_len);
num_dig = NaN(num_str,max_len);
for i = 1:num_str
num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
num_dig(i,z(i,:)) = last{i} - first{i} + 1;
end % Find columns that have at least one non-NaN. Make sure activecols is a
% 1-by-n vector even if n = 0.
activecols = reshape(find(~all(isnan(num_val))),1,[]);
n = length(activecols); % Compute which columns in the composite matrix get the numbers.
numcols = activecols + (1:2:2*n); % Compute which columns in the composite matrix get the number of digits.
ndigcols = numcols + 1; % Compute which columns in the composite matrix get chars.
charcols = true(1,max_len + 2*n);
charcols(numcols) = false;
charcols(ndigcols) = false; % Create and fill composite matrix, comp.
comp = zeros(num_str,max_len + 2*n);
comp(:,charcols) = double(s1);
comp(:,numcols) = num_val(:,activecols);
comp(:,ndigcols) = num_dig(:,activecols); % Sort rows of composite matrix and use index to sort c in ascending or
% descending order, depending on mode.
[unused,index] = sortrows(comp);
if is_descend
index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);

调用这个函数,在之前的代码中添加几行

clear;clc;close all
d = dir('tmp');
nameCell = cell(length(d)-2,1);
for i = 3:length(d)
disp(d(i).name)
nameCell{i-2} = d(i).name;
end
d2 = sort_nat(nameCell)

下面是见证奇迹的时刻

d2 =
'a1.txt'
'a2.txt'
'a3.txt'
'a11.txt'
'a12.txt'
'b10.txt'

这样就方便处理文件了。

参考:

http://cn.mathworks.com/matlabcentral/fileexchange/34464-customizable-natural-order-sort

matlab对文件目录进行自然排序的更多相关文章

  1. Java基础知识强化之集合框架笔记46:Set集合之TreeSet存储自定义对象并遍历练习2(自然排序:Comparable)

    1. TreeSet存储自定义对象并遍历练习2: (1)Student.java package cn.itcast_06; /* * 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口 * ...

  2. Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序:Comparable)

    1. 自然排序: TreeSet会调用集合元素的compareTo(Object obj)方法来比较元素之间的大小关系,然后将集合元素按照升序排列,这种方式就是自然排序. Java中提供了一个Comp ...

  3. Java基础知识强化之集合框架笔记44:Set集合之TreeSet保证元素唯一性和自然排序的原理和图解

    1. TreeSet保证元素唯一性和自然排序的原理和图解 2. TreeSet唯一性以及有序性底层剖析: 通过观察TreeSet的add()方法,我们知道最终要看TreeMap的put()方法. 跟踪 ...

  4. Collections之sort的两个方法(自然排序和自定义比较器排序)

    Collections是个服务于Collection的工具类(静态的),它里面定义了一些集合可以用到的方法. 本文演示了Collections类里sort()的两个方法.第一种只需传入被排序的集合,便 ...

  5. 浅析pinyin4j源码 简单利用pinyin4j对中文字符进行自然排序(转)

    pinyin4j项目  官网地址 http://pinyin4j.sourceforge.net/ 我们先把资源下载下来,连同源码和jar包一起放入工程.如下图: 接下来在demo包下,我们写一个测试 ...

  6. TreeSet集合排序方式一:自然排序Comparable

    TreeSet集合默认会进行排序.因此必须有排序,如果没有就会报类型转换异常. 自然排序 Person class->实现Comparable,实现compareTo()方法 package H ...

  7. 《java入门第一季》之集合框架TreeSet存储元素自然排序以及图解

    这一篇对TreeSet做介绍,先看一个简单的例子: * TreeSet:能够对元素按照某种规则进行排序.  * 排序有两种方式  * A:自然排序: 从小到大排序  * B:比较器排序    Comp ...

  8. TreeSet集合的自然排序与比较器排序、Comparable接口的compareTo()方法

    [自然排序] package com.hxl; public class Student implements Comparable<Student> { private String n ...

  9. TreeSet的自然排序(自定义对象 compareTo方法)

    >要实现自然排序,对象集合必须实现Comparable接口,并重写compareTo()方法 >一般需求中描述的是"主要条件",如:按姓名长度排序.  需注意次要条件 ...

随机推荐

  1. arcgis属性选取like用法

    查询对象为ArcInfo coverage,shapefile, INFO table,dBASE table,ArcSDE data,ArcIMS 要素类,或者 ArcIMS image servi ...

  2. linux优化之SElinux关闭

    查看selinux状态: # getenforce   注:Enforcing表示开启,Permissive表示禁用 临时关闭或开启selinux: # setenforce  [1|0]  注:1是 ...

  3. mysql varchar类型转换int类型找出最大值

    (1) 不严谨的,最简单的 select MAX(字段名 + 0) from 表名; (2) 使用函数实现 select MAX(cast(字段名 as SIGNED INTEGER)) from 表 ...

  4. 1.centOS安装Mysql

    上个星期研究了一个星期的Mysql,从今天起把学到的东西整理一下. ---------------------------------------------- mysql安装本人亲试过两种安装方式, ...

  5. TCP流量控制和拥塞控制

    TCP的流量控制      所谓的流量控制就是让发送方的发送速率不要太快,让接收方来得及接受.利用滑动窗口机制可以很方便的在TCP连接上实现对发送方的流量控制.TCP的窗口单位是字节,不是报文段,发送 ...

  6. PHP电商订单自动确认收货redis队列

    一.场景 之前做的电商平台,用户在收到货之后,大部分都不会主动的点击确认收货,导致给商家结款的时候,商家各种投诉,于是就根据需求,要做一个订单在发货之后的x天自动确认收货.所谓的订单自动确认收货,就是 ...

  7. Openstack(企业私有云)万里长征第一步——安装

    一.前言 单位新进了十几台服务器,建了一个高标准的一体化机房,状似刘姥姥进大观园的我,从机房规划到企业私有云搭建一一重头学来,除了机房泥墙其他基本都涉猎到了. 从企业私有云这个名字就能看出这是多么复杂 ...

  8. WBS任务分解中前置任务闭环回路检测:有向图的简单应用(C#)

    1 场景描述 系统中用到了进度计划编制功能,支持从project文件直接导入数据,并能够在系统中对wbs任务进行增.删.改操作.wbs任务分解中一个重要的概念就是前置任务,前置任务设置确定了不同任务项 ...

  9. 预览github项目的html文件新方法

    原文地址:→看过来 写在前面 关于如何在线预览github中的html文件,其实这是一个很多人知道的东西,但是查资料的时候呢总是找不到正确的答案,并且一开始我也是踩了坑的. 踩坑经历 搜出来的结果大概 ...

  10. 对JVM运行时常量池的一些理解

    1.JVM运行时常量池在内存的方法区中(在jdk8中,移除了方法区) 2.JVM运行时常量池中的内容主要是从各个类型的class文件的常量池中获取,对于字符串常量,可以调用intern方法人为添加,而 ...