我想創建3個列表,其中包含以png,jpg和bmp開頭的文件名。所有的文件都在目錄樹中的圖像:/ YEAR_MONTH/year_month_day /因爲你可以在這裏看到:創建一個表,其中包含從MATLAB中字符串中選擇的文件的特定名稱
./20160203/20160203_png01_test1.png.tmp
./20160203/20160203_jpg01_test2.jpg.tmp
./20160203/20160203_bmp01_test3.bmp.tmp
./20160204/20160204_png01_test1.png.tmp
./20160204/20160204_jpg01_test2.jpg.tmp
./20160204/20160204_bmp01_test3.bmp.tmp
./20160205/20160205_png01_test1.png.tmp
./20160205/20160205_jpg01_test2.jpg.tmp
./20160205/20160205_bmp01_test3.bmp.tmp
每個列表應包含圖片名稱爲每個擴展名(JPG,BMP和PNG),例如列表1應該有:
./20160203/20160203_png01_test1.png.tmp
./20160204/20160204_png01_test1.png.tmp
./20160205/20160205_png01_test1.png.tmp
list2中應該有:
./20160203/20160203_jpg01_test2.jpg.tmp
./20160204/20160204_jpg01_test2.jpg.tmp
./20160205/20160205_jpg01_test2.jpg.tmp
和項目list3相同,但與BMP ...
我的代碼如下所示:
workdir = '/home/user/201602/';
cd(workdir);
command = ['find -name *test*.tmp'];
[status,cmdout] = system(command);
c = strsplit(strtrim(cmdout));
n_files = length(c);
format_id = cell(n_files,1);
for ifile =1:n_files
d = textscan(c{ifile},'%s%s', 'Delimiter','_');
s1 = d{2}{:};
format_id{ifile} = s1;
end
format_id = unique(format_id);
nformat = length(format_id);
for iformat = 1:nformat
templist = [format_id{iformat} '.dat'];
fid = fopen(templist,'w');
for ifiles =1:n_files
fprintf(fid, '%6s\n', c{iformat});
end
fid = fclose(fid);
end
我得到的名單:png01.dat,jpg01.dat和bmp01.dat這是很好的,但在每個列表中,我都有圖片的所有名稱,而不僅僅是具有相應擴展名的名稱。首先,我想製作一個「索引」,以便僅選擇具有相應擴展名(bmp jpg和png)的文件,然後將它們發送到列表中。我怎麼能這樣做?
我希望問題清楚。在此先感謝您的幫助。
請在整個刪除其他新線你的問題,使它看起來更容易閱讀。作爲進一步的建議:查看['dir'](http://ch.mathworks.com/help/matlab/ref/dir.html) - 命令來替換'system'。 – Matt