試試這個:
%# 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替換爲適當的路徑,例如Linux上的/home/username/Documents/
或Windows上的C:\Windows\Blah\
。
我只測試了兩個測試的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
我剛剛更新了我的答案,因爲我意識到您的csv文件似乎有標題行。如果我的解決方案無法解決問題,請告訴我,因爲我必須在途中做出一些假設。我想最終的答案將會非常接近我現在所擁有的。 –