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

10 views (last 30 days)
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

Accepted Answer

dpb
dpb on 3 May 2022
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.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!