File 如何在Matlab中解析文件名和重命名

File 如何在Matlab中解析文件名和重命名,file,parsing,matlab,names,File,Parsing,Matlab,Names,我正在读一个.xls文件,然后在里面处理它,并在程序结束时重写它。我想知道是否有人能帮我分析一下日期 因为我的输入文件名类似于file_1_2010_03_03.csv 我希望我的输出文件是 新文件_2010_03_03.xls 有没有一种方法可以合并到matlab程序中,这样我就不必手动编写命令 xlswrite('newfile_2010_03_03.xls',M); 每次输入不同日期的文件时更改日期 喜欢 文件_2_2010_03_04.csv 也许我不清楚> 我正在使用uigetfile

我正在读一个.xls文件,然后在里面处理它,并在程序结束时重写它。我想知道是否有人能帮我分析一下日期 因为我的输入文件名类似于file_1_2010_03_03.csv

我希望我的输出文件是

新文件_2010_03_03.xls

有没有一种方法可以合并到matlab程序中,这样我就不必手动编写命令
xlswrite('newfile_2010_03_03.xls',M); 每次输入不同日期的文件时更改日期
喜欢 文件_2_2010_03_04.csv

也许我不清楚> 我正在使用uigetfile输入3个格式不同的文件 文件1\u 2010\u 03\u 03.csv、文件2\u 2010\u 03\u 03.csv、文件3\u 2010\u 03\u 03.csv

现在我正在处理程序中的文件,并编写4个输出文件 名称为newfileX_3_2010_03_03.xls、newfileXY_3_2010_03_03.xls、newfileXZ_3_2010_03.xls, 新文件yz_3_2010_03_03.xls

因此,我的日期不是当前日期,但我需要从输入文件中获取该日期,并将其附加到xlswrite的新名称中

所以我想知道是否有一种方法可以写一个泛型

xlswrite('xxx'M); 这将选择我想要的名称,而不是每次我输入新文件时让2修改名称“xxx”

谢谢


谢谢

我不知道您是否希望根据日期生成文件名。如果您只想更改所读取文件的名称,可以执行以下操作:

filename = 'file_1_2010_03_03.csv';
newfilename = strrep(filename,'file_1_', 'newfile_');
xlswrite(newfilename,M)
更新:

要从文件名分析日期,请执行以下操作:

dtstr = strrep(filename,'file_1_','');
dtstr = strrep(dtstr,'.csv','');
DT = datenum(dtstr,'yyyy_mm_dd');
disp(datestr(DT))
要基于日期生成文件名(例如今天的日期):


我不明白您是否希望根据日期生成文件名。如果您只想更改所读取文件的名称,可以执行以下操作:

filename = 'file_1_2010_03_03.csv';
newfilename = strrep(filename,'file_1_', 'newfile_');
xlswrite(newfilename,M)
更新:

要从文件名分析日期,请执行以下操作:

dtstr = strrep(filename,'file_1_','');
dtstr = strrep(dtstr,'.csv','');
DT = datenum(dtstr,'yyyy_mm_dd');
disp(datestr(DT))
要基于日期生成文件名(例如今天的日期):


看起来我误解了你对“文件1”、“文件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
%#加载文件,将名称放入单元格数组
文件名={'file_1_2010_03_03.csv'、'file_2_2010_03_03.csv'、'file_3_2010_03.csv';
%#解析数字和日期的文件名
%#此表达式查找n位数字(本例中为1、2或3)并将
%#它输入输出结构中的“number”字段,并查找日期
%#并将其放入输出结构中的“日期”字段中
%#具体而言,\d查找数字,\d+查找一个或多个数字,\ud+_
%#查找前跟下划线的一个或多个数字
%#_(?\d+)u查找前面和后面的一个或多个数字
%#并将它们(作为字符串)放入
%#输出结构。日期部分类似,只是regexp查找
%#特定位数
tmp=regexp(文件名,''name');
nameStruct=cat(1,tmp{:});%#regexp返回一个单元格数组。连环,便于使用
%#也许你想循环,也许不想(问题不太清楚),但是
%#下面是使用循环的方法。无论如何,由于文件名的相关信息
%#方便地存储在nameStruct中,您可以以任何方式访问它。
对于iFile=1:n文件
%#进行一些处理,得到矩阵M
%#并创建输出文件名
outputFileX=sprintf('newfileX\u%s\u%s.xls',名称结构(iFile).number,名称结构(iFile).date);
%#拯救
xlswrite(输出文件x,M)
结束
有关如何使用它们的更多详细信息,请参阅。此外,您可能对以下内容感兴趣:
替换uigetfile。

看起来我误解了你对“文件1”、“文件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
%#加载文件,将名称放入单元格数组
文件名={'file_1_2010_03_03.csv'、'file_2_2010_03_03.csv'、'file_3_2010_03.csv';
%#解析数字和日期的文件名
%#此表达式查找n位数字(本例中为1、2或3)并将
%#它输入输出结构中的“number”字段,并查找日期
%#并将其放入输出结构中的“日期”字段中
%#具体而言,\d查找数字,\d+查找一个或多个数字,\ud+_
%#查找前跟下划线的一个或多个数字
%#_(?\d+)u查找前面和后面的一个或多个数字
%#并将它们(作为字符串)放入
%#输出结构。日期部分类似,只是regexp查找
%#特定位数
tmp=regexp(文件名,''name');
nameStruct=cat(1,tmp{:});%#regexp返回一个单元格数组。连环,便于使用
%#也许你想循环,也许不想(问题不太清楚),但是
%#下面是使用循环的方法。无论如何,由于文件名的相关信息
%#方便地存储在nameStruct中,您可以以任何方式访问它。
对于iFile=1:n文件
%#进行一些处理,得到矩阵M
%#并创建输出文件名
outputFileX=sprintf('newfileX\u%s\u%s.xls',名称结构(iFile).number,名称结构(iFile).date);
%#拯救
xlswrite(输出文件x,M)
结束
有关如何使用它们的更多详细信息,请参阅。此外,您可能对以下内容感兴趣: 替换uigetfile。

如果所有文件中的3个文件名称中的日期相同,则您可以使用其中一个文件执行以下操作(处理完3个文件中的所有数据后):

该函数用于在出现
'
'.
字符的位置分解旧文件名。然后,该函数用于将日期的片段放在一起。

如果来自所有文件的3个文件都具有sa