dir函数的作用:返回当前路径中的所有文件以及文件夹所组成的列表。
dir %returns a list of files and folders in the current folder.类似于Dos命令中的DIR
dir name (或者 dir(name)) %returns a list of files and folders that match the string name;返回指定路径name所有文件及文件夹组成的列表;
listing = dir(name) % returns attributes about name;返回指定路径name属性的结构体类型的数组。即数组中每一个元素以结构体类型存储着每个子文件的信息。注意必须赋予变量。
例>>a=dir('my_dir')
61x1 struct array with fields:
name % 'my_dir'路径下所有文件的name列表
date %对应文件的创建日期
bytes
isdir
datenum
例,我们想知道该文件夹下第三个文件的名字,即就是访问该结构体中的name成员
>>a(3).name
ans = xxx.jpg %假设是文件名为xxx.jpg
具体细节可在matalb中输入>>doc dir 或者 help dir
sort 函数的用法
>> help sort
sort Sort in ascending or descending order.
For vectors, sort(X) sorts the elements of X in ascending order.
For matrices, sort(X) sorts each column of X in ascending order.
For N-D arrays, sort(X) sorts along the first non-singleton
dimension of X. When X is a cell array of strings, sort(X) sorts
the strings in ASCII dictionary order.
Y = sort(X,DIM,MODE)
has two optional parameters.
DIM selects a dimension along which to sort.%指定对哪个维度进行排序
MODE selects the direction of the sort. %指定是升序还是降序
'ascend' results in ascending order
'descend' results in descending order
The result is in Y which has the same shape and type as X.%返回结果的shape和类型与输入一致
[Y,I] = sort(X,DIM,MODE) also returns an index matrix I.%返回排序后的元素值和索引值,
If X is a vector, then Y = X(I).
If X is an m-by-n matrix and DIM=1, then
for j = 1:n, Y(:,j) = X(I(:,j),j); end
例>>X = [3 7 5; 0 4 2] %2rows 3cols
ans = 3 7 5
0 4 2
>>sort(X,1) %按第1个维度排序,由于matlab是按列优先存储,因此按列向量排序结果如下:
ans = 0 4 2
3 7 5
>>sort(X,2)
ans = 3 5 7
0 2 4
%%返回降序后的元素值和索引值
例 >> X=[8 4 6 2 1 5]
ans = 8 4 6 2 1 5
>>[value, index]=sort(x,'descend')
value = 8 6 5 4 2 1
index = 1 3 6 2 4 5
具体细节可在matalb中输入>>doc sort