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

조회 수: 12 (최근 30일)
greenyellow22
greenyellow22 2022년 5월 11일
답변: Star Strider 2022년 5월 11일
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
KSSV
KSSV 2022년 5월 11일
It would be better to show us your data and tell us your expectations.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 5월 11일
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'}
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Strategy & Logic에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by