试试这个:
%# Set the number of csv files
DirectoryPath = 'FullDirectoryPathHereWithTrailingSlash';
NumFile = 2;
%# Open the first file and get the first column (the date column)
File1Path = [DirectoryPath, 'PT_1.csv'];
fid1 = fopen(File1Path, 'r');
Date = textscan(fid1, '%s %*[^\n]', 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid1);
%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 second column and add it to the data matrix
for n = 1:NumFile
%# Get the current file name
CurFilePath = [DirectoryPath, 'PT_', num2str(n), '.csv'];
%# Open the current file for reading and scan in the second colum using numerical format
fid1 = fopen(CurFilePath, 'r');
CurData = textscan(fid1, '%*s %f %*[^\n]', 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid1);
%Add the current data to the cell array
D(:, n+1) = CurData{1, 1};
end
希望使用我提供的注释代码应该是不言自明的。有点棘手的是我在textscan
函数中使用的格式字符串。这是一个快速的解释:
1)'%s %*[^\n]'
说:获取第一列,它是字符串格式(即%s
)并跳过所有剩余的列(即%*[^\n]
)。
2)'%*s %f %*[^\n]'
说:跳过第一列,即字符串格式(即%*s
),获取第二列,即浮点数(即%f
),然后跳过所有剩余的列(即%*[^\n]
)。
更新:我刚刚更新了代码以在顶部包含一个变量,该变量允许您指定 csv 文件所在的目录(以防它不是当前目录)。只需将文本 FullDirectoryPathHereWithTrailingSlash 替换为适当的路径,例如/home/username/Documents/
在 Linux 或C:\Windows\Blah\
Windows 上。
我刚刚在两个测试 csv 文件上测试了这段代码,命名PT_1.csv
和PT_2.csv
看起来完全一样:
time, param 1, param 2, param 3
2/01/2001 23:00, 11, 3, 314.322471
3/01/2001 23:00, 12, 3, 311.683002
和
time, param 1, param 2, param 3
2/01/2001 23:00, 13, 0, 296.523937
3/01/2001 23:00, 14, 0, 294.0944
结果?
>> D
D =
730853 11 13
730854 12 14