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

조회 수: 9(최근 30일)
Dave
Dave 2021년 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