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

时间:2010-03-16 16:12:10

标签: file parsing matlab names

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

我希望我的输出文件是

newfile_2010_03_03.xls

有没有办法在matlab程序中加入,所以我不必手动编写命令
xlswrite('newfile_2010_03_03.xls',M); 每当我输入具有差异日期的文件时更改日期 喜欢 file_2_2010_03_04.csv。

也许我不清楚>   我正在使用uigetfile以格式输入3个diff文件 file_1_2010_03_03.csv,file_2_2010_03_03.csv,file_3_2010_03_03.csv

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

因此我的日期不是当前日期,但我需要从输入文件中将其添加到我的xlswrite的newname。

所以想知道我是否有办法写一个通用的

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

由于

由于

4 个答案:

答案 0 :(得分:1)

看起来我误解了你对'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

有关如何使用它们的详细信息,请参阅regular expressions。此外,您可能感兴趣 uipickfiles替换uigetfile。

答案 1 :(得分:0)

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

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_', datestr(date,'yyyy_mm_dd') '.csv'];

答案 2 :(得分:0)

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

fileName = 'file_1_2010_03_03.csv';          %# One of your 3 file names
data = textscan(fileName,'%s',...            %# Split string at '_' and '.'
                'Delimiter','_.');
fileString = sprintf('_%s_%s_%s.xls',..      %# Make the date part of the name
                     data{1}{(end-3):(end-1)});
xlswrite(['newfileX' fileString],dataX);     %# Output "X" data
xlswrite(['newfileXY' fileString],dataXY);   %# Output "XY" data
xlswrite(['newfileXZ' fileString],dataXZ);   %# Output "XZ" data
xlswrite(['newfileYZ' fileString],dataYZ);   %# Output "YZ" data

函数TEXTSCAN用于在出现'_''.'个字符的位置分解旧文件名。然后使用函数SPRINTF将日期的各个部分放在一起。

答案 3 :(得分:0)

据推测,所有这些文件都位于某个目录中,您希望批量处理它们。您可以使用这样的代码来读取特定目录中的文件,并找到以'csv'结尾的文件。这样,如果您想要处理新文件,则根本不需要更改代码 - 只需将其放在目录中并运行程序即可。

extension = 'csv';

files = dir();  % e.g. use current directory

% find files with the proper extension
extLength = length(extension);
for k = 1:length(files)
    nameLength = length(files(k).name);
    if nameLength > extLength
        if (files(k).name((nameLength - extLength + 1):nameLength) == extension)
            a(k).name
            % process file here...
        end
    end
end

通过结合Jonas建议的正则表达式处理,您可以使其更紧凑。