MATLAB Answers

How to use fillmissing with table variables of type cell

4 views (last 30 days)
012786534
012786534 on 29 Oct 2019
Commented: Guillaume on 29 Oct 2019
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 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 29 Oct 2019
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 Comments

012786534
012786534 on 29 Oct 2019
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 on 29 Oct 2019
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' }

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!