我正在读.xls文件,然后内部处理。第它在我的程序结束改写它。我想知道,如果有人可以帮我解析日期 作为我的输入的文件名是像file_1_2010_03_03.csv

和我希望我OUTPUTFILE是

newfile_2010_03_03.xls

有在Matlab程序掺入,所以我不必须手动写命令结果的方式 xlswrite( 'newfile_2010_03_03.xls',M); 每次和更改日期与DIFF日期我输入文件点击 喜欢 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

所以我的日期不是当前日期,但我需要从输入文件,并追加一条为newname我xlswrite。

所以不知道是否有办法可以写一个通用

xlswrite( 'XXX' M); 这将挑我想要的名称代替我具有2修改名称的“XXX”每次我输入一个新的文件

由于

由于

有帮助吗?

解决方案

它看起来像我误解你的意思与“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。

其他提示

如果您要根据日期或不建的文件名,我不明白。如果你只是想改变你读文件的名称,你可以这样做:

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'];

据推测,所有这些文件都是在某处坐在一个目录,你想处理这些批次。你可以用这样的代码来读取文件在一个特定的目录,并发现,在“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

可以通过结合乔纳斯建议的正则表达式处理使其更紧凑。

如果您的3个文件从 UIGETFILE 所有具有相同的日期在他们的名字,那么你可以只使用其中一个进行以下操作(你已经处理从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 然后用来把碎片为日期回到一起。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top