change table var type
163 views (last 30 days)
How can I change the variable type in a table from double to string ?
I found plenty of posts with the same issue but during import from excel. My table is already there and I need to change the var type of a column.
Steven Lord on 8 Feb 2021
A = array2table(magic(4))
A.Var1 = string(A.Var1)
Though depending what you're trying to do, using discretize or categorical to create a categorical array might be a better option.
patients = table(LastName,Gender,Age,Height,Weight);
patients.AgeCategory = discretize(patients.Age, 0:10:100, 'categorical');
patients.Gender = categorical(patients.Gender);
More Answers (1)
KALYAN ACHARJYA on 8 Feb 2021
Edited: KALYAN ACHARJYA on 8 Feb 2021
"My table is already there and I need to change the var type of a column."
If the data is numeric, you can use num2str(variable_name) to convert numeric var to string data type.
Lets suppose you have table variable T and want to convert string of the particular column variable "col1", then