1

私は気象データセットを扱っており、多くの csv ファイルから 1 つの列を抽出し、結果を新しいファイルにコンパイルする必要があります。1 か月間は動作しましたが、それよりも短い月になるとスクリプトが動かなくなります (添字の割り当ての次元が一致しません)。理にかなっていますが、プレースホルダー行列 D に元々存在する NaN 値を単純に保持したいのです。

これがスクリプトの問題部分です、

%Convert dates to matlab date numbers and get number of rows
Date = datenum(Date{1, 1}, 'dd-mm-yyyy');
T = size(Date, 1);    

%# Preallocate a matrix to hold all the data, and add the date column
D = [Date, NaN(T, NumFile)];

%# Loop over the csv files, get the eleventh column and add it to the data matrix
for k = 1:NumFile
FileList = dir('*.csv');
NumFile = size(FileList,1);
    filename = FileList(k).name;
    disp(filename);
    %# Get the current file name
    CurFilePath = filename;

    %# Open the current file for reading and scan in the second column using numerical format
    fid1 = fopen(CurFilePath, 'r');
    CurData = textscan(fid1, '%*s %*s %*s %*s %*s %*s %*s %*s %f %*[^\n]', 'Delimiter', ',"', 'HeaderLines', 17, 'MultipleDelimsAsOne',true);
    fclose(fid1);

    %Add the current data to the cell array
    D(:, k+1) = CurData{1, 1};
end

では、プレースホルダー行列 D に適合するように、短い月を 31 日月のサイズに強制するにはどうすればよいでしょうか。

4

1 に答える 1

3

1 つの次元でコロン演算子を代入Dする場合、Matlab は行内のすべての要素を代入していると想定する必要があります。これを修正するには、コロンを に交換するだけ1:numberOfDaysInMonthです。こうすると、Matlab は指定した数の値のみを割り当て、残りは変更しません。この場合は Nan です。

numberOfDaysInMonthとして計算できますsize(CurData{1, 1},1)

全体として、スクリプトの最後から 2 番目の行を次のように交換します。

%Add the current data to the cell array
numberOfDaysInMonth = size(CurData{1, 1},1);
D(1:numberOfDaysInMonth, k+1) = CurData{1, 1};
于 2013-01-09T19:55:36.287 に答える