Three ways to convert data types within a table or timetable
1. Use convertvars (R2018b) to convert table or timetable variables to another datatype. Example: Convert the first and last columns from a cellstring to string array.
T1 = readtable('outages.csv');
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ________________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'}
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'}
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
T2 = convertvars(T1,["Region","Cause"],"string");
head(T2,3)
Region OutageTime Loss Customers RestorationTime Cause
___________ ________________ ______ __________ ________________ ______________
"SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm"
"SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT "winter storm"
"SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 "winter storm"
2. Set the VariableTypes property of the table or timetable (R2024b)
Example: Convert the last names to strings and the Gender to categorical.
T1 = readtable("patients.xls");
head(T1,3)
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
{'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'}
{'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' }
{'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
T1.Properties.VariableTypes([1,2]) = ["string","categorical"];
head(T1,3)
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus
__________ ______ ___ _____________________________ ______ ______ ______ ________ _________ ________________________
"Smith" Male 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'}
"Johnson" Male 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' }
"Williams" Female 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' }
Tip: To replace all "cell" types to "string", use:
T1.Properties.VariableTypes = strrep(T1.Properties.VariableTypes,'cell','string');
3. Reassign the table variable
Example: Convert the "Cause" variable from cellstring to categorical.
T1 = readtable('outages.csv');
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ________________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm'}
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm'}
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm'}
T1.Cause = categorical(T1.Cause);
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause
_____________ ________________ ______ __________ ________________ ____________
{'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 winter storm
{'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT winter storm
{'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 winter storm
T1.Region = convertCharsToStrings(T1.Region);
head(T1,3)
Region OutageTime Loss Customers RestorationTime Cause
___________ ________________ ______ __________ ________________ ____________
"SouthWest" 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 winter storm
"SouthEast" 2003-01-23 00:49 530.14 2.1204e+05 NaT winter storm
"SouthEast" 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 winter storm