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?

 채택된 답변

Sean de Wolski
Sean de Wolski 2017년 7월 7일

6 개 추천

Change it before reading it in by modifying the options passed to readtable.
Or
t.Var1 = num2str(t.Var1)

댓글 수: 2

Nikhil Tawakley
Nikhil Tawakley 2017년 7월 7일
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 2017년 7월 7일
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).

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

추가 답변 (2개)

Jeffrey Daniels
Jeffrey Daniels 2020년 6월 4일

6 개 추천

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);
Peter Perkins
Peter Perkins 2017년 7월 7일

0 개 추천

readtable also accepts a Format parameter that would allow you to override the automatic type detection.

댓글 수: 4

Thanks Peter. Is there a way to copy formats from one table to another? I want to take all the datatypes/formatting of one table and mimic it in another.
I tried
table2.Variable_1_Name.Formats=table1.Variable_1_Name.Formats
but that returned an error: "Field assignment to a non-structure array object."
Another approach could be to modify table2 when importing: (https://www.mathworks.com/help/matlab/import_export/import-mixed-text-and-numeric-data-from-a-text-file.html)
However, I don't know how to get the formatSpec argument out of table1 and don't want to type it out manually.
-Nikhil
Peter Perkins
Peter Perkins 2017년 8월 16일
I don't know what this means. Tables don't have formats.
Nikhil Tawakley
Nikhil Tawakley 2017년 8월 29일
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?
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) );

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

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

태그

질문:

2017년 7월 7일

댓글:

2020년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by