필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by