MATLAB Answers


converting cell to categorical in table

Asked by corroMat on 18 Jan 2019
Latest activity Commented on by corroMat on 23 Jan 2019 at 15:44
I'm new to using tables, and I have a large table where I have a variable made up mainly of numbers (integers) and some values like '115b', which are then read in as a cell in the table.
I can't have it as all double and it can't stay as a cell as i need to be able to treat the contents the same. I can convert it to categorical as the numbers are integers, reoccurring and not too expansive.
The numbers in the cells are causing errors so I've tried to convert to cell and then reinsert into the table but it's looking complicated - there must be a better way?
Is there a quick way of converting this column to categorical? This is my convoluted method so far...
% temppivot is a table of doubles and cells
varType1 = varfun(@class,tempPivot,'OutputFormat','cell'); % checks for variable type
hdgs = tempPivot.Properties.VariableNames; % gets headings
for ii = 1:size(varType1,2)
if strcmp(varType1{ii}, 'cell') % if a cell then...
tempPivot = convertvars(tempPivot,hdgs{ii},'categorical'); % convert to categorical (works in most cases as the vars are text based
catch errmsg
allErrors{ii} = errmsg;
if strcmp(errmsg.identifier,'MATLAB:categorical:UniqueMethodFailedData')
disp('Converting partial numerical data to categorical')
out=cellfun(@num2str,tempPivot.(hdgs{ii}),'un',0); %converts to strings
out = table(out,'VariableNames',hdgs(ii)); %converts back to table
% % need to put the cell back into the table but thought i'd ask for a simpler route before figuring this one out.
Here are some of the errors I've gotten
tempPivot = convertvars(tempPivot,hdgs{ii},'categorical');
>Error using categorical (line 356)
>Could not find unique values in DATA using the UNIQUE function.
>Error in tabular/convertvars (line 72)
> a = a.subsasgnDot(vars(i),type({vars(i)}));
>Caused by:
> Error using cell/unique (line 85)
> Cell array input must be a cell array of character vectors. % because there are doubles
tempPivot = convertvars(out,hdgs{ii},'categorical');
>Undefined function 'convertvars' for input arguments of type 'cell'. %where out is cell as result of cellfun/num2str (not a table)
Thanks in advance!


Sign in to comment.

1 Answer

Answer by Peter Perkins
on 23 Jan 2019 at 14:35

It sounds like what you have is a cell array like {1 'two' 3 'four'}. It would help to see a short example to confirm that.
Your instinct to convert to categorical is probably a good one, but it's hard for me to say because you don't say what you want things like 115b to become. Perhaps those are hex numbers. So converting to numeric might be another choice.
In any case, the first error you are seeing is because there is very little support for "mixed" cell arrays. Mostly only for cell arrays of char row vectors ("cellstrs"). It's not clear how you ended up where you are, but my first suggestion is to find the root cause and fix it there. Barring that, you can patch this up in several steps. Understanding what the following code does, and tailoring it to what you actually need to do, will be a useful learning experience:
>> c = {1 'two' 3 'four'};
>> isNum = cellfun(@isnumeric,c,'UniformOutput',true)
isNum =
1×4 logical array
1 0 1 0
>> c1 = c; c1(~isNum) = { NaN }; c1 = cell2mat(c1)
c1 =
1 NaN 3 NaN
>> c2 = categorical(c1,1:4,{'a' 'b' 'c' 'd'})
c2 =
1×4 categorical array
a <undefined> c <undefined>
>> c2(~isNum) = c(~isNum)
c2 =
1×4 categorical array
a two c four
>> categories(c2)
ans =
6×1 cell array
{'a' }
{'b' }
{'c' }
{'d' }
{'two' }
Maybe you then want to combine the 'b' and 'two categories, etc., maybe not. Up to you.
convertvars is a table function. Have to use it on a table, not on raw variables. In any case, it won't do what you are hoping unles you write your own function similar to the above.

  1 Comment

Thank you for your answer. It's clearly not very easy to do want I want! I just ended up finishing the loop and it does work ok.

Sign in to comment.