1

私は .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 のような差分日付のファイルを入力するときに日付を変更します。

たぶん私は明確ではありませんでした> file_1_2010_03_03.csv、file_2_2010_03_03.csv、file_3_2010_03_03.csvの形式で3つの差分ファイルを入力するためにuigetfileを使用しています

現在、プログラム内でファイルを処理し、newfileX_3_2010_03_03.xls、newfileXY_3_2010_03_03.xls、newfileXZ_3_2010_03_03.xls、newfileYZ_3_2010_03_03.xls という名前の 4 つの出力ファイルを書き込んでいます。

したがって、私の日付は現在の日付ではありませんが、入力ファイルからそれが必要であり、それを xlswrite の newname に追加します。

だから私はジェネリックを書くことができる方法があるかどうか疑問に思っていました

xlswrite ('xxx' M); 新しいファイルを入力するたびに名前「xxx」を変更する代わりに、必要な名前を選択します

ありがとう

ありがとう

4

4 に答える 4

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

使用方法の詳細については、正規表現を参照してください。また、 uigetfileを置き換えるuipickfilesに興味があるかもしれません。

于 2010-03-16T16:27:48.003 に答える
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'];
于 2010-03-16T16:25:21.367 に答える
0

UIGETFILEの 3 つのファイルの名前がす​​べて同じ日付である場合は、そのうちの 1 つを使用して次の操作を実行できます (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を使用して、日付の断片を元に戻します。

于 2010-03-16T16:41:51.983 に答える
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 が提案した正規表現処理を組み込むことで、よりコンパクトにすることができます。

于 2010-03-16T16:50:35.943 に答える