一:文件移動\復(fù)制
movefile
% 從father目錄中復(fù)制指定類型的文件到目錄s中
father='H:前期測試3'; %指定類型的文件所在的目錄
s='H:前期測試3.3'; %復(fù)制文件的目標(biāo)目錄
subDir=dir(father); %求目錄的子目錄
len = length(subDir); %求子目錄的長度
disp('begin copy files..');
for i=3:len
imgNames = dir(strcat(father,subDir(i).name,'','*.JPEG'));
a=[s,subDir(i).name,''];
mkdir([s,subDir(i).name])
for j=1:20 %復(fù)制的文件個數(shù)
movefile([father,subDir(i).name,'',imgNames(j).name],a);
end
end
disp('end');
end
copyfile
% 從father目錄中復(fù)制指定類型的文件到目錄s中
father='H:前期測試3'; %指定類型的文件所在的目錄
s='H:前期測試3.3'; %復(fù)制文件的目標(biāo)目錄
subDir=dir(father); %求目錄的子目錄
len = length(subDir); %求子目錄的長度
disp('begin copy files..');
for i=3:len
imgNames = dir(strcat(father,subDir(i).name,'','*.JPEG'));
a=[s,subDir(i).name,''];
mkdir([s,subDir(i).name])
for j=1:20 %復(fù)制的文件個數(shù)
copyfile([father,subDir(i).name,'',imgNames(j).name],a);
end
end
disp('end');
end
- movefile和copyfile的重要區(qū)別
clear
clc
cd('C:\Documents and Settings\Administrator\桌面\matlab\test');
% 設(shè)置當(dāng)前目錄
% 此時test文件夾中有:文件夾1, 文件夾2篮绿, 文件1.txt, 文件2.txt
movefile('1.txt', '11.txt'); % 把1.txt剪切成11.txt(1.txt不存在了)
%實際上相當(dāng)于改名
copyfile('2.txt', '22.txt'); % 把2.txt復(fù)制成22.txt(2.txt依然存在)
movefile('11.txt', '1'); % 把11.txt剪切到文件夾1中
copyfile('22.txt', '2'); % 把22.txt復(fù)制到文件夾2中
- 指定路徑下 單個文件夾data中所有圖像文件
file_path ='.\data\'; % 圖像文件夾路徑
img_path_list = dir(strcat(file_path,'*.jpg'));%獲取該文件夾中所有jpg格式的圖像
img_num = length(img_path_list);%獲取圖像總數(shù)量
if img_num > 0 %有滿足條件的圖像
for j = 1:img_num %逐一讀取圖像
image_name = img_path_list(j).name;% 圖像名
image = imread(strcat(file_path,image_name));
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 顯示正在處理的圖像名
%圖像處理過程 省略
end
end
注: 上述的代碼只能讀取data文件夾中的圖像,假設(shè)data中包含子文件夾揪罕,不能讀取子文件夾中的圖像饰躲。
三、多種功能代碼
- 指定路徑下 多個文件夾中所有圖像
該代碼可以讀取文件夾data中杀餐,及data的所有子文件夾中的圖像。
p = genpath('.\data');% 獲得文件夾data下所有子文件的路徑,這些路徑存在字符串p中嫉沽,以';'分割
length_p = size(p,2);%字符串p的長度
path = {};%建立一個單元數(shù)組,數(shù)組的每個單元中包含一個目錄
temp = [];
for i = 1:length_p %尋找分割符';'俏竞,一旦找到绸硕,則將路徑temp寫入path數(shù)組中
if p(i) ~= ';'
temp = [temp p(i)];
else
temp = [temp '\']; %在路徑的最后加入 '\'
path = [path ; temp];
temp = [];
end
end
clear p length_p temp;
%至此獲得data文件夾及其所有子文件夾(及子文件夾的子文件夾)的路徑堂竟,存于數(shù)組path中。
%下面是逐一文件夾中讀取圖像
file_num = size(path,1);% 子文件夾的個數(shù)
for i = 1:file_num
file_path = path{i}; % 圖像文件夾路徑
img_path_list = dir(strcat(file_path,'*.jpg'));
img_num = length(img_path_list); %該文件夾中圖像數(shù)量
if img_num > 0
for j = 1:img_num
image_name = img_path_list(j).name;% 圖像名
image = imread(strcat(file_path,image_name));
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 顯示正在處理的路徑和圖像名
%圖像處理過程 省略
end
end
end
- matlab中讀取一行多個字符的文本
fid = fopen('');
while ~feof(fid)
tline=fgetl(fid);
[row col] = size(tline);
print = findstr(tline,'print');
vein = findstr(tline,'vein') ;
user_id = findstr(tline,'user_id');
p_value = str2num(tline(1,print+6:vein-2));
v_value = str2num(tline(1,vein+5:user_id-2));
plot(p_value,v_value,'r*');
end;
- matlab讀取文檔后截取特定字符
fid = fopen('D:\360Downloads\11\result.txt');
tline=fgetl(fid);
[row col] = size(tline);
print = findstr(tline,'print');
vein = findstr(tline,'vein') ;
user_id = findstr(tline,'user_id:44 ');
p_value = str2num(tline(1,print+6:vein-2));
v_value = str2num(tline(1,vein+5:user_id-2));
- matlab新建文件夾及copy文件
clc ;
clear ;
for k=1:50
failname = dir('C:\Documents and Settings\Administrator\桌面\users\*.*') ;
[row col ] = size(failname);
for i =3:row
path = ['C:\Documents and Settings\Administrator\桌面\users\' failname(i).name] ;
str = ['ui_' num2str(8*(k-1)+i-2)];
savepath = ['C:\Documents and Settings\Administrator\桌面\usersfft2\' str] ;
copyfile(path,savepath);
end;
end;
figure(1)
- matlab copy整個文件夾及以下內(nèi)容
[row col ] = size(failname);
for i = 1:row
path = failname{i} ;
len = length(path);
startpoint = len ;
while(path(startpoint)~='\')
startpoint = startpoint - 1 ;
end;
str = [] ;
for i = startpoint+1:len
str = [str path(i)] ;
end
savepath = ['D:\failimage\' str] ;
copyfile(path,savepath);
end;
figure
- matlab中cell的讀取
names={'fyc','hy','ljg','lqf','lsl','ml','nhz','rj','syj','wl','wq','wyc','xch','xxj','yjf',
'zc','zdx','zjg','zl','zyf'};
len_names=length(names);
for i=1:len_name
surl=strcat('D:\GaitDatasetA-silh\silhouettes\',names(i),'\00_1');
url=url{1};
將cell類型轉(zhuǎn)換為string類型end
正如上面所示的一樣玻佩,url調(diào)用strcat('D:\GaitDatasetA-silh\silhouettes',names(i),'\00_1');得到的是一個cell類型的變量出嘹,此時需要對url做一些變換就可以了,使用url=url{1};就搞定了咬崔!
- matlab讀文檔中的字符
clc
filename =fopen('E:\\filename.txt','r');%打開當(dāng)前目錄下的文檔shiyan.txt里面存的是4*3數(shù)組
fid = fgetl(filename);
image1=imread(fid);
fd=fopen('E:\\points.txt','r');%//打開當(dāng)前目錄下的文檔shiyan.txt里面存的是4*3數(shù)組
A=fscanf(fd,'%f');%//讀取這12個數(shù)據(jù)税稼,保存在向量A中
for j=1:4
for i=1:2
B(j,i)=A((j-1)*2+i);%//轉(zhuǎn)移到4*3矩陣B中
end;
end;
figure(1)subplot(1,2,2);
imshow(image1);
hold onfor j=1:4
plot(B(j,1),480-B(j,2),'*');
end;
hold off;
filename =fopen('E:\\filename_ir.txt','r');%打開當(dāng)前目錄下的文檔shiyan.txt里面存的是4*3數(shù)組
fid = fgetl(filename);
image1=imread(fid);
fd=fopen('E:\\points_ir.txt','r');%//打開當(dāng)前目錄下的文檔shiyan.txt里面存的是4*3數(shù)組
A=fscanf(fd,'%f');%//讀取這12個數(shù)據(jù),保存在向量A中
for j=1:4
for i=1:2
B(j,i)=A((j-1)*2+i);%//轉(zhuǎn)移到4*3矩陣B中
end;
end;
subplot(1,2,1);imshow(image1);
hold onfor j=1:4
plot(B(j,1),480-B(j,2),'*');
end;
hold off;
- 在matlab運算中使用未知字符
可以先用syms聲明該字符垮斯,然后就可以在計算中使用該字符郎仆,例如:
>> syms a;
>> b=[cos(a) sin(a);-sin(a) cos(a)]
b =
[ cos(a), sin(a)]
[ -sin(a), cos(a)]
>> inv(b)
ans =
[ cos(a)/(cos(a)^2+sin(a)^2), -sin(a)/(cos(a)^2+sin(a)^2)]
[ sin(a)/(cos(a)^2+sin(a)^2), cos(a)/(cos(a)^2+sin(a)^2)]
- 將采到的樣本按用戶名重新存儲
clc ;
folder_path = 'G:\1\';save_path = 'G:\1_rename\' ;
folders = dir(folder_path);
[row_folder col_folder] = size(folders);
for i=3:row_folder
tmp_folderpath = [folder_path folders(i).name] ;
tmp_folderpath = [tmp_folderpath '\'] ;
info_path = [tmp_folderpath 'user.info'] ;
fid = fopen(info_path,'rb');
while ~feof(fid)
tline=fgetl(fid);
t = findstr(tline,'caption');
if(t)
[row1 col1] = size(tline);
tt = tline(9:col1);
save_path1 = [save_path tt] ;%新的文件夾名
save_path1 =[save_path1 '\'] ;%新的文件夾名
break;
end;
end;
fclose(fid); %%%%%copy 文件
source_path = [tmp_folderpath 'sample\'] ;
copyfile(source_path,save_path1);
% tmp = [source_path '*.bmp'] ;
% tmp2 = dir(tmp);
% [row1 col1] = size(tmp2) ;
% for j=1:row1
% source_path = [source_path tmp2(j).name] ;
% savepath1 = [savepath tmp2(j).name] ;
% copyfile(source_path,savepath1);
% end;
end;
[Ref] (http://blog.csdn.net/flyingworm_eley/article/details/6644968#)