フィルターのクリア

merging column elements into one

2 ビュー (過去 30 日間)
MF
MF 2016 年 3 月 28 日
回答済み: Roger Stafford 2016 年 3 月 28 日
I have a matrix of 4 columns containing year in 1st column, month in 2nd, day in 3rd, and time in 4th. Is there a way to combine them into 1 column to get 'dd-mmm-yyyy HH:MM:SS' ?
Thanks
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 3 月 28 日
What is the datatype of your matrix? If it is a cell, then what is the format of each of the columns at present?
MF
MF 2016 年 3 月 28 日
編集済み: MF 2016 年 3 月 28 日
double. For example:
year month day hour
2015 2 1 1

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

回答 (4 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 28 日
Look at this example
v={'2016' '03' '02' '12:10:00';'2016' '03' '02' '13:20:00'}
out=arrayfun(@(x) [v{x,1} '-' v{x,2} '-' v{x,3} ' ' v{x,4}] , (1:size(v,1))','un',0)
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 28 日
編集済み: Azzi Abdelmalek 2016 年 3 月 28 日
v=[2015 2 1 1;2016 2 10 2]
w=[v zeros(size(v,1),2)]
out=datestr(w,'yyyy-mm-dd HH:MM:SS')

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


Walter Roberson
Walter Roberson 2016 年 3 月 28 日
v={'2016' '03' '02' '12:10:00';'2016' '03' '02' '13:20:00'}
strcat(v(:,1),{'-'},v(:,2),{'-'},v(:,3), {' '}, v(:,4))
but only if v happens to be in the correct format already, which is something we do not know yet as you have not replied about what the format of your array is.
  1 件のコメント
MF
MF 2016 年 3 月 28 日
my v is a 9834x5 double matrix

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


Stephen23
Stephen23 2016 年 3 月 28 日
編集済み: Stephen23 2016 年 3 月 28 日
Use datstr and cellstr:
>> M = [2016,03,28,12;2015,2,1,1]
M =
2016 3 28 12
2015 2 1 1
>> M(:,6) = 0;
>> C = cellstr(datestr(M,'dd-mmm-yyyy HH:MM:SS'));
>> C{:}
ans = 28-Mar-2016 12:00:00
ans = 01-Feb-2015 01:00:00
You can read the documentation and pick the date format that suits your needs best.

Roger Stafford
Roger Stafford 2016 年 3 月 28 日
You can easily compress seconds, minutes, hours, days of the month, months, and years into one double precision floating point number. It has more than enough capacity. Let T be a vector with six elements: [year number, month number, day of month number, hour, minutes, seconds]. To convert to a single 'double':
d = T(6)+64*(T(5)+64*(T(4)+64*(T(3)+64*(T(2)+64*T(1)))));
(There is room here to go up past the year 8000000.) To convert back to T vector:
function T = dec2base64(d)
T = zeros(1,6);
for k = 6:-1:2
q = floor(d/64);
T(k) = d - 64*q;
d = q;
end
T(1) = d;
return

カテゴリ

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