filepath直接指定到文件名吗_按照txt中指定的文件名,从src_path中拷贝文件到dest_path(copyfile_from_txt)...

function num_processed = copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name)

% Author:shizhixin

% Email:szhixin@gmail.com

% Blog:http://blog.csdn.net/shizhixin

% Date:2012-03-02

% Function:按照txt中指定的文件名,从src_path中拷贝文件到dest_path文件夹下

% Note: txt文件格式如下,

% no obj_fitsname

% 1 spec-SSY02_1_sp07-156

% 从第二行start_row=2开始,第二列为文件名(find space position)

% Example:函数可以拷贝如spec-SSY02_1_sp07-156-[1...n].png的文件

% 若src_path中有如下文件:

% spec-SSY02_1_sp07-156-1.png

% spec-SSY02_1_sp07-156-2.png

% spec-SSY02_1_sp07-156.fits

%

% add_num = 0;

% ext_name = '.fits'

% 则拷贝spec-SSY02_1_sp07-156.fits文件

%

% add_num = 2;

% ext_name = '.png'

% 则拷贝spec-SSY02_1_sp07-156-1.png和spec-SSY02_1_sp07-156-2.png文件

% add_num = 2;

% ext_name = '.png'

% txt_filename = 'H:\20120301_peakfit_result\20120301_ssy_two_lines_10122\obj_filename.txt';

% src_path = 'H:\20120301_peakfit_result\20120301_ssy_two_lines_10122\dbpeakfit_png\';

% dest_path = 'test2\';

%copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name)

start_row = 2;

num_processed = 0;

[file_info] = importdata(txt_filename);

len = length(file_info)

for i=start_row:len

try

str = file_info{i};

pos = find(isspace(str)==1);%找到空格位置,即第二列的文件名

filename = str(pos+1:end);

filename = strtrim(filename);

if add_num==0

copy_name = [filename ext_name];

copyfile([src_path copy_name], [dest_path copy_name]);

else

for j=1:add_num

copy_name = [filename '-' num2str(j) ext_name];

copyfile([src_path copy_name], [dest_path copy_name]);

end

end%if

num_processed = num_processed+1;

catch

disp(lasterr)

end %try ... catch

end %for

end%function

运行结果: add_num = 2;  ext_name = '.png'  txt_filename = 'H:\obj_filename.txt';  src_path = 'H:\dbpeakfit_png\';  dest_path = 'test\'; copyfile_from_txt(txt_filename,src_path,dest_path,add_num,ext_name) ext_name = .png len =    234 %文件名不存在会报错,但是不会终止程序运行结束 Error using ==> copyfile No matching files were found. Error using ==> copyfile No matching files were found. ans =    231

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值