How to split a column in a table into two columns by a space

12 ビュー (過去 30 日間)
JFz
JFz 2016 年 3 月 4 日
コメント済み: JFz 2016 年 3 月 7 日
I have a table of 4 columns. One column is datetime, '2016-02-01 20:48:58.0000000' I would like to split the date and time into two columns. How to do that? Thanks!
Jennifer

採用された回答

Image Analyst
Image Analyst 2016 年 3 月 4 日
Here's one way:
% Create table.
for k = 1 : 3
ca{k} = datestr(now)
pause(1.1)
end
% Create table with two columns, initialized to the same for now.
t = table(ca', ca')
% NOW START:
% Split column #1 into columns 1 and 2.
for k = 1 : size(t, 1)
thisString = cell2mat(t{k,1});
t{k, 1} = {thisString(1:11)};
t{k, 2} = {thisString(13:end)};
end
% Show new t in command window.
t
  1 件のコメント
JFz
JFz 2016 年 3 月 7 日
Thanks. It works! But now, my table has thousands of rows. Is there a way to make a varfun or something similar to split that column to make it run fast?

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

その他の回答 (2 件)

Ahmet Cecen
Ahmet Cecen 2016 年 3 月 4 日
  1 件のコメント
JFz
JFz 2016 年 3 月 4 日
Thanks. Should I apply this as a function to the column?

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


Peter Perkins
Peter Perkins 2016 年 3 月 7 日
If the variable in your table is literally a datetime, and not a cellstr containing datestrs, then you'll want to split them uing timeofday to get the time part, and dateshift to get the date part.
  1 件のコメント
JFz
JFz 2016 年 3 月 7 日
Thanks! I will try that. In the mean time, is there anyway to avoid the for loop, such as something like varfun to make it run in a blast?

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by