How to work with categorical or "cell" variables within a table?

12 views (last 30 days)
Hello! I am new in Matlab so this might be a basic question but I hope that you can help me anyway! :)
I loaded some gaming data as a table in matlab which contains columns such as "Date", "Time", and "Status" such as "Game is loading..." or "life point collected". When I asked for the class of data, each variable was labelled as being a "cell" type.
RandomNo Date Time Status
__________ ______________ ________________ _______________________________________________________________
1.345e+12 {'10/04/2021'} {'17:57:55:890'} {'Saving successful.'}
1.674e+12 {'10/04/2021'} {'17:57:55:890'} {'New Game File created successfully.'}
...
So my question is: how can I convert the variables in a way that I can work with them? For example, to calculate the min or max Date/Time, I would first convert both cell variables into datetime but I only got error messages.
>> time_stamps = datetime(gamingdata{:,3}, 'InputFormat', 'HH:mm:ss.SSS');
Error using datetime
Unable to convert the text to datetime using the format 'HH:mm:ss.SSS'.
OR
>> time_stamps = datetime(gamingdata{:,Time}, 'InputFormat', 'HH:mm:ss.SSS');
'Time' requires Embedded Coder.
Thanks in advance!

Accepted Answer

Star Strider
Star Strider on 11 May 2022
Converting the ‘Date’ and ‘Time’ variables to datetime arrays is straightforward, however it is not obvious.
Try this —
T1 = table(rand(2,1)*1E12, [{'10/04/2021'}; {'10/04/2021'}], [{'17:57:55:890'}; {'17:57:55:890'}],[{'Saving'};{'Saving Successful'}], 'VariableNames',{'RandomNr','Date','Time','Status'})
T1 = 2×4 table
RandomNr Date Time Status __________ ______________ ________________ _____________________ 8.6947e+11 {'10/04/2021'} {'17:57:55:890'} {'Saving' } 5.6831e+11 {'10/04/2021'} {'17:57:55:890'} {'Saving Successful'}
T1.DateTime = datetime(T1.Date,'InputFormat','dd/MM/yyyy') + timeofday(datetime(T1.Time,'InputFormat','HH:mm:ss:SSS'))
T1 = 2×5 table
RandomNr Date Time Status DateTime __________ ______________ ________________ _____________________ ____________________ 8.6947e+11 {'10/04/2021'} {'17:57:55:890'} {'Saving' } 10-Apr-2021 17:57:55 5.6831e+11 {'10/04/2021'} {'17:57:55:890'} {'Saving Successful'} 10-Apr-2021 17:57:55
T1 = removevars(T1,{'Date','Time'})
T1 = 2×3 table
RandomNr Status DateTime __________ _____________________ ____________________ 8.6947e+11 {'Saving' } 10-Apr-2021 17:57:55 5.6831e+11 {'Saving Successful'} 10-Apr-2021 17:57:55
T1 = T1(:,[1 3 2])
T1 = 2×3 table
RandomNr DateTime Status __________ ____________________ _____________________ 8.6947e+11 10-Apr-2021 17:57:55 {'Saving' } 5.6831e+11 10-Apr-2021 17:57:55 {'Saving Successful'}
.

More Answers (0)

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!