How to replace all elements in a Table?

38 ビュー (過去 30 日間)
Leon
Leon 2020 年 3 月 14 日
コメント済み: Ameer Hamza 2020 年 3 月 14 日
For example, if I want to replace all -9999 with -999, I can do this in a matrix:
A(A==-9999) = -999;
My question is how to do this in a table that is generated by readtable?
A = readtable('test.xlsx');
When I tried, I got an error saying:
Operator '==' is not supported for operands of type 'table'.
Thank you.
  1 件のコメント
dpb
dpb 2020 年 3 月 14 日
Read the doc on accessing data in a table ...

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 14 日
Accessing the elements of the table are different as compared to the matrix. One solution is to convert the table to an array and then do the replacement. In the end, convert the array back to MATLAB: https://www.mathworks.com/matlabcentral/answers/450426-replacing-elements-in-a-table. You can make replacement without doing the conversion to the matrix as follow.
num_vars = size(T, 2);
for i=1:num_vars
T{:,i}(T{:,i}==-9999) = -999;
end
However, this will work as long as the class of every column in the table supports the comparison with double datatype using == operator.
  2 件のコメント
Leon
Leon 2020 年 3 月 14 日
Many thanks!
I do have unknow number of columns that can be text strings. Is there an easy way to implement it in that case?
Ameer Hamza
Ameer Hamza 2020 年 3 月 14 日
In that case, try this
for i=1:num_vars
if isa(T{:,i}, 'double')
T{:,i}(T{:,i}==1) = 100;
end
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by