Replacing Empty Cells by NaN

82 ビュー (過去 30 日間)
Muhammad Farooq Zia
Muhammad Farooq Zia 2021 年 8 月 22 日
編集済み: Scott MacKenzie 2021 年 8 月 22 日
Hi Matlab Community,
I would appreciate some help. I am importing an excel .csv file into a table. This file has multiple cells as empty cells. However, once imported as table in Matlab, some empty cells are filled with NaN while others show {0×0 char}. (Refer below)
I want to replace all empty cells in the table with NaN value, because using table2array function with {0×0 char} gives an error, which is as follows:
Error using table2array (line 37)
Unable to concatenate the table variables 'Var1' and 'Var16', because their types are double and cell.
Error in Berea_NTW1 (line 42)
node1 = table2array(node1Table) ;
Thanks a lot !!
  1 件のコメント
Ive J
Ive J 2021 年 8 月 22 日
Woud you mind to share your data file? use paper clip to upload.

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

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 8 月 22 日
編集済み: Scott MacKenzie 2021 年 8 月 22 日
You can use fillmissing and specify the fill value for each column. Obviously, the fill values depend on the data type in the column. Here's an example:
T = table({'a' '' 'f' 'z'}', [1 2 nan 4]', [9 nan nan 5]', {'z' '' '' ''}')
T = 4×4 table
Var1 Var2 Var3 Var4 __________ ____ ____ __________ {'a' } 1 9 {'z' } {0×0 char} 2 NaN {0×0 char} {'f' } NaN NaN {0×0 char} {'z' } 4 5 {0×0 char}
T = fillmissing(T, 'constant', {'Oops!', 0, -1, 'Unknown'})
T = 4×4 table
Var1 Var2 Var3 Var4 _________ ____ ____ ___________ {'a' } 1 9 {'z' } {'Oops!'} 2 -1 {'Unknown'} {'f' } 0 -1 {'Unknown'} {'z' } 4 5 {'Unknown'}
  2 件のコメント
Muhammad Farooq Zia
Muhammad Farooq Zia 2021 年 8 月 22 日
Thank you for your reply. I tried putting NaN as the constant value in fillmissing function, but still its not working. This is the code line where I try this:
node1Table = fillmissing(node1Table, 'constant', NaN) ;
can you please tell me how I can resolve this?
Scott MacKenzie
Scott MacKenzie 2021 年 8 月 22 日
編集済み: Scott MacKenzie 2021 年 8 月 22 日
You don't insert NaN in fillmissing. You insert the value you want to substitute for NaN, such as 0. Just look at my example code.
Also, since the columns in your table have different data types, you need to specify the replacement values according to the type of data in each column; i.e., you cannot use NaN as the replacement value for a column that contains char data. Again, see my example code.

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

その他の回答 (1 件)

Simon Chan
Simon Chan 2021 年 8 月 22 日
T.Var1=NaN(size(T.Var1,1),1); % Replace T.Var1 by NaN
T.Var16=NaN(size(T.Var16,1),1); % Replace T.Var16 by NaN

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by