how to rename or move files

Hello,
I want to rename or move files from one name to another in the same folder. i write following script. but it gives errors.
clear all;
close all;
year=1998;
mn=1;
for dd=1:9
display(['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc']);
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc' 'dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
end
>>mytest
dt_ref_med_merged_madt_h_19980101.nc
??? Error using ==> movefile
Can not copy or move a file or directory onto itself.
Error in ==> mytest at 10
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc' 'dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
how can I fix it ?
thanks.

回答(1 个)

per isakson
per isakson 2014-8-28
编辑:per isakson 2014-8-30
"from one name to another in the same folder." &nbsp I cannot see that you provide "another name"
movefile (['dt_ref_med_merged_madt_h_',num2str(year),'0',num2str(mn),'0',num2str(dd),'.nc']...
, ['dt_ref_med_merged_madt_h_',num2str(year),'_',num2str(dd),'.nc']);
"|],[|" in the middle (to split the unreadable code into two names) was missing.
&nbsp
EDIT: I find this easier to read
y = 2014;
m = 1;
d = 23;
sprintf( 'dt_ref_med_merged_madt_h_%s.nc' ...
, datestr([y,m,d,0,0,0],'yyyymmdd') )
ans =
dt_ref_med_merged_madt_h_20140123.nc

类别

在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息

提问:

2014-8-28

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by