天天看点

matlab 复制文件夹,Matlab文件及文件夹操作典型代码(收录)

一:文件移动\复制

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;