How to split only one column of a table (based on a space)?

조회 수: 3 (최근 30일)
greenyellow22
greenyellow22 2022년 5월 3일
답변: dpb 2022년 5월 3일
Hey!
I want to split only ONE column of a table I inserted into matlab. In this column, date and time (e.g., 22/06/2022 13:45:33) is seperated only by one space. I want to split this colum into two seperate ones, so one for date and one for time.
I appreciate any help! Thanks

채택된 답변

dpb
dpb 2022년 5월 3일
If it's still in text form (string or cellstr), it's no problem
dt=split(tTable.DateString); % split the component pieces
tTable.Date=dt(:,1); % add Date column
tTable.Time=dt(:,2); % add Time column
Both are still whatever string type started out with.
Then it's trickier and splitting may not be the best idea after all -- in MATLAB datetime class, a date can exist with a presumed time of midnight but a time cannot exist without a corresponding date; there is no such thing as a standalone absolute time in this implementation.
You either turn time into a duration of some ilk or keep the date and simply change the display format to display the time portion alone.
A better and more complete answer could depend upon what the intent of the above is to accomplish -- it may not be necessary (probably isn't) to create the two variables anyway to perform whatever tasks are intended.
Another alternative could be to use a timetable instead where the date/time is inherent in the row structure and there are some specialized tools for common tasks that may well solve the problem.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by