How to convert cell to cell array of character vectors.

103 vues (au cours des 30 derniers jours)
Blue
Blue le 5 Août 2019
Réponse apportée : Blue le 14 Août 2019
Hi,
So I have a basic table that looks like this but has over 60 000 rows:
A = [48.44, 48.16].';
B = ['A', 'B'].';
C = table(A, B);
The problem is that when I type unique(C.B) Matlab complains : Cell array input must be a cell array of character vectors.cell array of character vectors.
The commands class(btl_data.POLYGON) returns 'cell', and iscellstr(btl_data.POLYGON) returns 0. The commands char(C.B) and cellstr(C.B) fail (Element 1 is not a string scalar or character array. All elements of cell input must be string scalars or character arrays.)
So I dont know. I know this is really basic but nothing works. Column B really should be only characters.
Thoughts ?
  3 commentaires
Guillaume
Guillaume le 13 Août 2019
Note that with your example, B is not a cell array. It's a 2x1 char vector, and unique would work fine with that.
Proper demo data:
A = [48.44, 48.16].';
B = {'A', 'BB'}.'; %with vectors of varying size you would not have been able to create a char matrix
C = table(A, B);
clearly, if iscellstr returns false, then as Adam explains, there's a non char vector in your cell array (maybe [] instead ''?). As the error message tells you, it's at least the first element that is a problem.
Stephen23
Stephen23 le 14 Août 2019
Note that this line
B = ['A', 'B'].';
is unlikely to be useful. Square brackets are a concatenation operator, so this
['A', 'B']
is exactly equivalent to this:
'AB'

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 13 Août 2019
Modifié(e) : Adam Danz le 13 Août 2019
At least one of the elements in btl_data.POLYGON is not a character vector. This demo below recreates the error.
A = [48.44, 48.16, 50].';
B = ['A', 'B', {5:10}].';
C = table(A, B);
unique(C.B)
% Error using cell/unique (line 85)
% Cell array input must be a cell array of character vectors.
To find the row numbers of table C that do not contain a char vector in column "B",
notChar_rowIndex = find(~cellfun(@ischar,C.B))
% ans =
% 3
Now you can look at those rows,
C(notChar_rowIndex,:)
  1 commentaire
Guillaume
Guillaume le 13 Août 2019
The error message says it's the first element that's problematic. There may be more so there's indeed value in getting these row indices.

Connectez-vous pour commenter.

Plus de réponses (1)

Blue
Blue le 14 Août 2019
Thank you all for your input.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by