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

12 vues (au cours des 30 derniers jours)
greenyellow22
greenyellow22 le 11 Mai 2022
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!
  2 commentaires
KSSV
KSSV le 11 Mai 2022
It would be better to show us your data and tell us your expectations.
greenyellow22
greenyellow22 le 11 Mai 2022
I did above

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 11 Mai 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'}
.

Plus de réponses (0)

Catégories

En savoir plus sur Strategy & Logic dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by