How to use fillmissing with table variables of type cell
조회 수: 2 (최근 30일)
이전 댓글 표시
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,
댓글 수: 0
답변 (1개)
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
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 Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!