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

7 views (last 30 days)
Dave on 15 Oct 2021
Answered: Image Analyst on 15 Oct 2021
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 Comment
Jan on 15 Oct 2021
What is the contents of T.var3? Is it a string or a numerical vector?

Sign in to comment.

Answers (2)

Cris LaPierre
Cris LaPierre on 15 Oct 2021
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 on 15 Oct 2021
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!