dir函数可以有调用方式为:
dir('.') 列出当前目录下所有子文件夹和文件
dir('G:\Matlab') 列出指定目录下所有子文件夹和文件
dir('*.m') 列出当前目录下符合正则表达式的文件夹和文件
得到的为结构体数组每个元素都是如下形式的结构体:
name
-- filename
date
-- modification
date
bytes
-- number of bytes allocated to the
file
isdir
-- 1 if name is a directory and 0 if
not
datenum --
modification date as a MATLAB serial date number
分别为文件名,修改日期,大小,是否为目录,Matlab特定的修改日期
可以提取出文件名以作读取和保存用.
2.
比如说在E:\new下有一系列txt文件,文件名符合matlab变量名的命名规则,并且没有temp.txt文件,每个文件有两列,以空格分割,第一列是字符串,第二列是浮点数字,要读取第二列的浮点数字为数组并以文件名为变量名。
file=dir('E:\new\*.txt');
for
n=1:length(file)
temp=dlmread(['E:\new\',file(n).name],' ',0,1);
eval_r([file(n).name(1:end-4),'=temp;'])
end
================
自己的‘丑’代码(ReadFolder):
clc;
clear;
%指定将要读的文件夹的路径!! 下面两条语句需要调整
walkcircle_dir = dir('D:\Caltech101\camera\');
cd 'D:\Caltech101\camera\';
start = 3;
last = 52; %利用win7支持的文件夹改名,改成最简单的形式。然后再利用自己的程序:)
这里指定一个Image数量就好
for i = start : last
temp =
walkcircle_dir(i).name;
I = imread(temp);
str = ''; n = 1;
j = 1;
while
temp(j)>='0' &
temp(j)<='9'
str(n) = temp(j);
j = j + 1;
n = n + 1;
end
j = j + 2;
%跳过‘ ’和'(' ; 前面一串字符
str2 = ''; n = 1;
%(后面的一串字符
while
temp(j)>='0' &
temp(j)<='9'
str2(n) = temp(j);
j = j + 1;
n = n + 1;
end
% 额, 特殊的处理~~
x = str2num(str2);
if x <
10
str(length(str)+1) = '0';
end
t1 = strcat(str, str2);
% 拼接在一起
newname
= strcat(t1, '.jpg');
imwrite(I, newname);
end