質問

私は、.xlsファイルを読み込み、その後、内部のそれをprocesing、私のプログラムの最後でそれを書き換えています。誰かが日付を解析するために私を助けることができる場合、私は思っていました 私の入力ファイル名がfile_1_2010_03_03.csvのようなものであるように、

と私は私のOUTPUTFILEになりたい

newfile_2010_03_03.xls

私は手動でコマンドを記述する必要はありませんので、MathWorks社のMATLABプログラムに組み込む方法はあり
xlswrite( 'newfile_2010_03_03.xls'、M)。 毎回と差分日付のI入力ファイルが
として日付を変更 好む file_2_2010_03_04.csvます。

たぶん私>は明確ではありませんでした   私は入力する形式の3つの差分ファイルをuigetfileは使用しています 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)。 これは私が名前を変更2を持つのではなく、私が欲しいの名前を選択します「XXX」毎回私入力新しいファイル

おかげ

おかげ

役に立ちましたか?

解決

私は数字1と2は、重要性のいくつかの種類を持っていたと思った。

-

これは、私はあなたが「file_1」、「file_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)

UPDATE:

ファイル名から日付を解析するには:

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