看起来我误解了您对“file_1”、“file_2”的含义——我认为数字 1 和 2 具有某种重要性。
oldFileName = 'something_2010_03_03.csv';
%# extract the date (it's returned in a cell array
theDate = regexp(oldFileName,'(\d{4}_\d{2}_\d{2})','match');
newFileName = sprintf('newfile_%s.xls',theDate{1});
带说明的旧版本
我假设您所有文件中的日期都是相同的。所以你的程序会去
%# load the files, put the names into a cell array
fileNames = {'file_1_2010_03_03.csv','file_2_2010_03_03.csv','file_3_2010_03_03.csv'};
%# parse the file names for the number and the date
%# This expression looks for the n-digit number (1,2, or 3 in your case) and puts
%# it into the field 'number' in the output structure, and it looks for the date
%# and puts it into the field 'date' in the output structure
%# Specifically, \d finds digits, \d+ finds one or several digits, _\d+_
%# finds one or several digits that are preceded and followed by an underscore
%# _(?<number>\d+)_ finds one or several digits that are preceded and follewed
%# by an underscore and puts them (as a string) into the field 'number' in the
%# output structure. The date part is similar, except that regexp looks for
%# specific numbers of digits
tmp = regexp(fileNames,'_(?<number>\d+)_(?<date>\d{4}_\d{2}_\d{2})','names');
nameStruct = cat(1,tmp{:}); %# regexp returns a cell array. Catenate for ease of use
%# maybe you want to loop, or maybe not (it's not quite clear from the question), but
%# here's how you'd do with a loop. Anyway, since the information about the filenames
%# is conveniently stored in nameStruct, you can access it any way you want.
for iFile =1:nFiles
%# do some processing, get the matrix M
%# and create the output file name
outputFileX = sprintf('newfileX_%s_%s.xls',nameStruct(iFile).number,nameStruct(iFile).date);
%# and save
xlswrite(outputFileX,M)
end
有关如何使用它们的更多详细信息,请参阅正则表达式。此外,您可能对用
uipickfiles替换 uigetfile 感兴趣。