我正在使用气象数据集,需要从许多 csv 文件中提取一列并将结果编译到一个新文件中。我让它工作了一个月,但脚本在遇到更短的月份时卡住了(下标分配维度不匹配)。有道理,但是,我希望它简单地保留占位符矩阵 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
那么,如何才能将较短的月份强制为 31 天月份的大小以适应占位符矩阵 D。