Wildcard '*' not working for "isfile" or renaming using "move file" commands.

20 ビュー (過去 30 日間)
Brian Bartlett
Brian Bartlett 2021 年 6 月 14 日
コメント済み: Brian Bartlett 2021 年 6 月 14 日
I am trying to use a for loop to rename multiple netcdf files based on the dates within their filenames ex:
my filenames are "SAMETEXTFORALL_2018MMDD_DifferentTextForAll.nc"
and I need to get them to be "nest_1_2018MMDD000000.nc"
I am trying to use a for loop such as the following for each month:
for x=0:9;
if isfile(['SAMETEXTFORALL_2018010',num2str(x),'_*.nc'])==1;
movefile(['SAMETEXTFORALL_2018010',num2str(x),'_*.nc'], ['nest_1_2018010',num2str(x),'000000.nc']);
but the isfile is returning a 0 for each despite the file existing, and when I do the movefiles individually, they create new folders with the filename, but keep the original filename the same. I think the issue lies within the '*' wildcard, because when I put in the appropriate name for individual files it works. Any help/suggestions? There are too many files to do this on a more individual basis. Thanks!


Gatech AE
Gatech AE 2021 年 6 月 14 日
I frequently need all of the FITS image files from a directory, and so I've gotten used to using the "dir" function. It seems like you could use the wildcard below. "dir" outputs a structure of various information, but we can just get names directly by dot indexing "name." Then you can parse out the MMDD and do everything at once.
filelist = {dir('SAMETEXTFORALL_2018*.nc').name};
for fn = 1:length(filelist)
name = filelist{fn};
ind = strfind(name,'2018');
MMDD = name((ind+4):(ind+7));
modName = ['nest_1_2018' MMDD '000000.nc'];
  1 件のコメント
Brian Bartlett
Brian Bartlett 2021 年 6 月 14 日
Yes! This was incredibly helpful, thank you so much.


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by