フィルターのクリア

Assign dates from cell array to matrix elements

2 ビュー (過去 30 日間)
John Petersen
John Petersen 2012 年 4 月 26 日
I have a file I'll call testdoc.csv. It's in the format below:
header
site, date, time, parm1, parm2
1098,2/23/2012,0:00,18.5,20.6
1098,2/23/2012,1:00,18.5,20.6
1098,2/23/2012,2:00,18.5,20.6
1098,2/23/2012,3:00,18.5,20.7
I want to extract the date and time and put it in a six column vector. The "time" in the file is HH:MM. The procedure I have adopted is
d = importdata('testdoc.csv')
d =
data: [4x4 double]
textdata: {6x7 cell}
The textdata cell comes out like so
Columns 1 through 6
'header' [] [] [] []
'site' ' date' ' time' ' parm1' ' parm2'
'1098' '2/23/2012' '0:00' '' ''
'1098' '2/23/2012' '6:00' '' ''
'1098' '2/23/2012' '12:00' '' ''
'1098' '2/23/2012' '18:00' '' ''
I want to move the date and time to a matrix like
A = [day month year hours min sec]
so the first two rows in this case would be
2 23 2012 0 0 0
2 23 2012 6 0 0
Is there a way to re-assign the cell array to a matrix? I've tried
A = datevec([d.textdata{3:end,2} ' ' d.textdata{3:end,3}])
which works for a single element, but not for a vector of elements. I really want to avoid loops because of huge files.

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 4 月 26 日
d1 = d.textdata(3:end,2:3)
out = datevec(strcat(d1(:,1),d1(:,2)),'mm/dd/yyyyHH:MM');
  1 件のコメント
John Petersen
John Petersen 2012 年 4 月 26 日
Perfect. THANKYOU!

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 4 月 26 日
C = textdata(3:end, 2:3);
S = sprintf('%s ', transpose(C));
D = sscanf(S, '%d/%d/%d %d:%d');
D = transpose(reshape(D, 5, []));
D(:, 6) = 0;
  1 件のコメント
John Petersen
John Petersen 2012 年 4 月 26 日
>> S=sprintf('%s ',transpose(C));
Error using sprintf
Function is not defined for 'cell' inputs.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by