How to replace a string with another string in a Matlab table

Hi,
I have a table that has 'N/A' in some of the column cells. I want to replace 'N/A' by ''. Would anyone tell me how to do that?
Thanks,
Jennifer

 採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 28 日

4 投票

Example:
t.Var1(strcmp(t.Var1,'N/A')) = {''};

6 件のコメント

JFz
JFz 2015 年 8 月 28 日
編集済み: JFz 2015 年 8 月 28 日
Thank you! This works! Super!
Ashishkumar Gupta
Ashishkumar Gupta 2023 年 1 月 7 日
@Walter Roberson Hi, what if I do not the variable and want to check entire table of size 4000x20.
I want to replace ' **** ' with ' 0 '. The table contains date, time, numbers.
Thanks!!!
Walter Roberson
Walter Roberson 2023 年 1 月 7 日
' **** ' cannot occur in variables of type date, time, or numeric. It could occur in char and string entries (categorical too I suppose.)
If you have **** in input fields that you are expecting to be dates or times or numbers, then readtable() would already have converted those to NaT (Not A Time) or NaN (duration) or NaN (numeric). You would work with those after reading by using fillmissing
You can also use table options at the time of readtable() to specifically indicate that '****' is to be treated as missing data -- you might need to do that if a lot of the earlier rows were missing, to prevent readtable() from guessing that the column is text instead of whatever is appropriate.
Ashishkumar Gupta
Ashishkumar Gupta 2023 年 1 月 7 日
編集済み: Ashishkumar Gupta 2023 年 1 月 7 日
@Walter Roberson It did not convert '****' with NaN. Also, I already have a line ( data_read = fillmissing(data_read,'constant',0,'DataVariables',@isnumeric);) to fill NaN with 0.
I've attached the screenshot of table. Please provide some code/hint to remove it,.... Thanks
Stephen23
Stephen23 2023 年 1 月 7 日
編集済み: Stephen23 2023 年 1 月 7 日
"Please provide some code/hint to remove it"
Rather than removing them, the best approach is to avoid them in the first place (hint: as Walter Roberson already wrote, you can use the READTABLE options, e.g. TreatAsMissing). If you clicked the paperclip button to upload a sample data file then someone could probably help you with that.
Ashishkumar Gupta
Ashishkumar Gupta 2023 年 1 月 7 日
Thanks @Stephen23, Treatasmissing worked....I did not know the exactword to write in readtable(), that's y could not understand @Walter Roberson.
Now it worked!!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 8 月 27 日

1 投票

Look at the isnan() function and I'm sure you'll find the way.

カテゴリ

ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索

製品

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by