Change table variable data type

370 visualizaciones (últimos 30 días)
Nikhil Tawakley
Nikhil Tawakley el 7 de Jul. de 2017
Comentada: yunyu Hu el 5 de Nov. de 2020
I used readtable() to read a text file into a table. One of the columns of the table was read (by default) as a double rather than a string. How does one change a columns datatype within a table?

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 7 de Jul. de 2017
Change it before reading it in by modifying the options passed to readtable.
Or
t.Var1 = num2str(t.Var1)
  2 comentarios
Nikhil Tawakley
Nikhil Tawakley el 7 de Jul. de 2017
The approach to modifying the read options was what I tried first but unfortunately detectimportoptions doesnt seem to work with 2016a...
num2str worked though so the issue is resolved...thanks!
Peter Perkins
Peter Perkins el 7 de Jul. de 2017
Without meaning to contradict Sean, num2str is almost certainly not what you want, at least not by itself. It is a very old function that creates a char matrix, which will likely be pretty awkward to work with. One possibility is to apply cellstr to that char matrix, another (if you're using a recent version of MATLAB) would be to convert using the string function (as in, string, the new class).

Iniciar sesión para comentar.

Más respuestas (2)

Jeffrey Daniels
Jeffrey Daniels el 4 de Jun. de 2020
fileName = table.xlsx;
opts = detectImportOptions(fileName);
opts.VariableTypes{15} = 'char'; % This will change column 15 from 'double' or whatever it is to 'char', which is what you want.
new_table = readtable(fileName,opts);
  1 comentario
yunyu Hu
yunyu Hu el 5 de Nov. de 2020
This is a simple and clean solution.

Iniciar sesión para comentar.


Peter Perkins
Peter Perkins el 7 de Jul. de 2017
readtable also accepts a Format parameter that would allow you to override the automatic type detection.
  4 comentarios
Nikhil Tawakley
Nikhil Tawakley el 29 de Ag. de 2017
I'm referring to the data types of the variables in a table. For example, a Table with variables: Name and Age with datatypes String and Integer respectively.
Is it possible to "copy" the datatypes from one table and then use them while creating another table? Continuing from the example above, if there's another table with variables: Item Name & Quantity, is it possible to "copy-paste" the datatypes String and Integer from the previous table?
Walter Roberson
Walter Roberson el 29 de Ag. de 2017
No, it is not possible to copy-paste datatypes. However, you could use
old_class = arrayfun(@class, OldTable.VariableName, 'Uniform', 0);
new_variable = arrayfun(@(value, newclass) cast(value, newclass), NewTable.VariableName, old_class, 'uniform', 0);
NewTable.VariableName = new_variable;
This assumes that each entry in the variable is a potentially different type -- which would not be the case for numeric entries for example. If you have numeric entries, then
NewTable.VariableName = cast( NewTable.VariableName, class(OldTable.VariableName) );

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by