1. 我写的 ls.py
2. matlab 命令 textscan
1.
E:\python>python ls.py -a E:\python ls.txt
输出绝对路径到 ls.txt
Usage: ls.py [options] path outfile
Options:
-h, --help show this help message and exit
-a ABSOLUTE, --absolute=ABSOLUTE
0/1, Output Absolute Path, Default [0]
-f FILE, --file=FILE 0/1, List files. Default [1]
-d DIR, --dir=DIR 0/1, List directories. Default [1]
-e EXT, --ext=EXT Only list pathes with some extension. If only files
with an extension are needed, you have to specify -d 0
-e ext
2.
c=textscan(fid,'%s');
会将所有的路径读入到 1x1 cell c
c{1}{n} 为 nth 路径
附 textscan 说明
textscan 按格式读入文本,生成 cell
c=textscan(fid,'%s%d%s');
则 c 为 3x1 cell,假设共有 n 个 records
则 c{1} 为 nx1 cell (这是 string 的特殊之处)
则 c{2} 为 nx1 matrix
则 c{3} 为 nx1 cell
clear
DATA = 'D:\dataset\orl_faces';
myls = 'E:\python\mytoolbox\ls.py';
cmd = (['python ' myls ' -a 1 -f 0 ' DATA ' file']);
unix(cmd)
fid = fopen('file');
class = textscan(fid,'%s');
class = class{1};
fclose(fid);
for c=1:length(class)
cmd = (['python ' myls ' -a 1 -d 0 -e pgm ' class{c} ' pic.txt']);
unix(cmd)
fid = fopen('pic.txt');
tmp = textscan(fid,'%s');
pic{c} = tmp{1};
fclose(fid);
end
save('pic','pic');
若要将每个类的图片名存在一个文件,文件名和类名相同,则采用如下代码clear
DATA = 'D:\dataset\orl_faces';
myls = 'E:\python\mytoolbox\ls.py';
% absolute
cmd = (['python ' myls ' -a 1 -f 0 ' DATA ' file']);
unix(cmd)
% relative, ie, class name
cmd = (['python ' myls ' -a 0 -f 0 ' DATA ' class']);
unix(cmd)
fid = fopen('file');
file = textscan(fid,'%s');
file = file{1};
fclose(fid);
fid = fopen('class');
class = textscan(fid,'%s');
class = class{1};
fclose(fid);
for c=1:length(class)
cmd = (['python ' myls ' -a 1 -d 0 -e pgm ' file{c} ' ' class{c} '.txt']);
unix(cmd)
end
0 comments:
Post a Comment