In table, replace NaN with Text (from NaN to "ABCD")

7 次查看(过去 30 天)
Dave
Dave2021-10-15
回答: Image Analyst ,2021-10-15
Hello, In a column of a table, is there a non-loop way to replace the NaN for any fixed text say "ABCD" ?
In the column that I need the change, it is either NaN or it is text (no numeric)
I tried
T.var3(ismissing(T.var3)) = "ABCD";
But it does not change it to ABCD, it keeps the NaN
also tried
T.var3(isnan(T.var3)) = "ABCD";
  1 个评论
Jan
Jan 2021-10-15
What is the contents of T.var3? Is it a string or a numerical vector?

请先登录,再进行评论。

回答(2 个)

Cris LaPierre
Cris LaPierre 2021-10-15
NaN suggests you have numeric data in your column (Not A Number). You will have to convert your numeric values to strings if you want to have "ABCD" be a value in your variable.

Image Analyst
Image Analyst 2021-10-15
Like Jan says, you can't do it. Numbers are numbers and characters/string are characters/strings. You could do it with a cell array, but not a table. If the item is a NaN, then that means that column is all numbers. String columns will never be a NaN so that's how I know. And you cannot set an item in a numerical column of a table to a string. You can only replace it with numbers because it's a numerical column. The column would have to be strings, not numbers, if you wanted to stuff "ABCD" into it.
I think you really need to decide what made this table and how they got to be nan's before you can start deciding how to repair the table. Please attach the table in a .mat file.
save('answers.mat', 'T');

Community Treasure Hunt

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

Start Hunting!

Translated by