フィルターのクリア

How to use fillmissing with table variables of type cell

5 ビュー (過去 30 日間)
012786534
012786534 2019 年 10 月 29 日
コメント済み: Guillaume 2019 年 10 月 29 日
Hello,
I simply want to replace all empty cells ([]) by '99' in the table attached but only for the variables that begin with Q_. I get the following error however: Table variables of type cell are not supported.How do I get around that ?
all_vars = T1.Properties.VariableNames;
index = find(strncmp(all_vars, 'Q_', 2));
T1(:, index) = fillmissing(T1(:, index), 'constant', '99')
Thank you,

回答 (1 件)

Guillaume
Guillaume 2019 年 10 月 29 日
Well, you're a funny one!
You've asked a very similar question. You were given two answers, one of which used fillmissing to do exactly what you're asking now, but you accepted the other answer.
Note that my answer used a simpler way of finding the data variables starting with Q_. For a start the find serves no purpose in the code you show. You'd get the exact same result using
index = strncmp(all_vars, 'Q_', 2); %no need for find, you can use the logical vector directly
T(:, index) = ...
but using startsWith as per my answer is even simpler than strncmp.
  2 件のコメント
012786534
012786534 2019 年 10 月 29 日
Hi Guillaume,
Yes, I did ask a very similar question. However I get an error with your answer (for this question here) : Table variables of type cell are not supported.
missinglocs = ismissing(T1, '[]');
newt = fillmissing(T1, 'constant', '-99', 'DataVariables', startsWith(T1.Properties.VariableNames, 'Q_'), 'MissingLocations', missinglocs)
Or am I doing something wrong ?
Thank you for your help,
Guillaume
Guillaume 2019 年 10 月 29 日
Which version of matlab are you using?
I don't get any error using R2019b:
>> a = {'A'; 'B'; 'C'};
o = {'A'; '[]'; 'C'};
Q_y = {'1'; '[]'; '3'};
T1 = table(Q_x, a, o, Q_y)
T1 =
3×4 table
Q_x a o Q_y
______ _____ ______ ______
{'1' } {'A'} {'A' } {'1' }
{'[]'} {'B'} {'[]'} {'[]'}
{'3' } {'C'} {'C' } {'3' }
>> missinglocs = ismissing(T1, '[]');
newt = fillmissing(T1, 'constant', '-99', 'DataVariables', startsWith(T1.Properties.VariableNames, 'Q_'), 'MissingLocations', missinglocs)
newt =
3×4 table
Q_x a o Q_y
_______ _____ ______ _______
{'1' } {'A'} {'A' } {'1' }
{'-99'} {'B'} {'[]'} {'-99'}
{'3' } {'C'} {'C' } {'3' }

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by