一:檔案移動\複制
movefile
% 從father目錄中複制指定類型的檔案到目錄s中
father='H:前期測試3'; %指定類型的檔案所在的目錄
s='H:前期測試3.3'; %複制檔案的目标目錄
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 %複制的檔案個數
movefile([father,subDir(i).name,'',imgNames(j).name],a);
end
end
disp('end');
end
copyfile
% 從father目錄中複制指定類型的檔案到目錄s中
father='H:前期測試3'; %指定類型的檔案所在的目錄
s='H:前期測試3.3'; %複制檔案的目标目錄
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 %複制的檔案個數
copyfile([father,subDir(i).name,'',imgNames(j).name],a);
end
end
disp('end');
end
movefile和copyfile的重要差別
clear
clc
cd('C:\Documents and Settings\Administrator\桌面\matlab\test');
% 設定目前目錄
% 此時test檔案夾中有:檔案夾1, 檔案夾2, 檔案1.txt, 檔案2.txt
movefile('1.txt', '11.txt'); % 把1.txt剪切成11.txt(1.txt不存在了)
%實際上相當于改名
copyfile('2.txt', '22.txt'); % 把2.txt複制成22.txt(2.txt依然存在)
movefile('11.txt', '1'); % 把11.txt剪切到檔案夾1中
copyfile('22.txt', '2'); % 把22.txt複制到檔案夾2中
指定路徑下 單個檔案夾data中所有圖像檔案
file_path ='.\data\'; % 圖像檔案夾路徑
img_path_list = dir(strcat(file_path,'*.jpg'));%擷取該檔案夾中所有jpg格式的圖像
img_num = length(img_path_list);%擷取圖像總數量
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檔案夾中的圖像,假設data中包含子檔案夾,不能讀取子檔案夾中的圖像。
三、多種功能代碼
指定路徑下 多個檔案夾中所有圖像
該代碼可以讀取檔案夾data中,及data的所有子檔案夾中的圖像。
p = genpath('.\data');% 獲得檔案夾data下所有子檔案的路徑,這些路徑存在字元串p中,以';'分割
length_p = size(p,2);%字元串p的長度
path = {};%建立一個單元數組,數組的每個單元中包含一個目錄
temp = [];
for i = 1:length_p %尋找分割符';',一旦找到,則将路徑temp寫入path數組中
if p(i) ~= ';'
temp = [temp p(i)];
else
temp = [temp '\']; %在路徑的最後加入 '\'
path = [path ; temp];
temp = [];
end
end
clear p length_p temp;
%至此獲得data檔案夾及其所有子檔案夾(及子檔案夾的子檔案夾)的路徑,存于數組path中。
%下面是逐一檔案夾中讀取圖像
file_num = size(path,1);% 子檔案夾的個數
for i = 1:file_num
file_path = path{i}; % 圖像檔案夾路徑
img_path_list = dir(strcat(file_path,'*.jpg'));
img_num = length(img_path_list); %該檔案夾中圖像數量
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整個檔案夾及以下内容
[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類型轉換為string類型end
正如上面所示的一樣,url調用strcat('D:\GaitDatasetA-silh\silhouettes',names(i),'\00_1');得到的是一個cell類型的變量,此時需要對url做一些變換就可以了,使用url=url{1};就搞定了!
matlab讀文檔中的字元
clc
filename =fopen('E:\\filename.txt','r');%打開目前目錄下的文檔shiyan.txt裡面存的是4*3數組
fid = fgetl(filename);
image1=imread(fid);
fd=fopen('E:\\points.txt','r');%//打開目前目錄下的文檔shiyan.txt裡面存的是4*3數組
A=fscanf(fd,'%f');%//讀取這12個資料,儲存在向量A中
for j=1:4
for i=1:2
B(j,i)=A((j-1)*2+i);%//轉移到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');%打開目前目錄下的文檔shiyan.txt裡面存的是4*3數組
fid = fgetl(filename);
image1=imread(fid);
fd=fopen('E:\\points_ir.txt','r');%//打開目前目錄下的文檔shiyan.txt裡面存的是4*3數組
A=fscanf(fd,'%f');%//讀取這12個資料,儲存在向量A中
for j=1:4
for i=1:2
B(j,i)=A((j-1)*2+i);%//轉移到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;